第四十节 通过type创建复杂的类,元类应用

def upper_attr(class_name, class_parents, class_attr):
                # Foo       object         {'bar':"bip"}
    ''' 将所有类属性或者方法的名字改成大写'''
    new_attr = {}
    for name, value in class_attr.items():
        if not name.startswith('__'):
            new_attr[name.upper()] = value
    return type(class_name, class_parents, new_attr)


class Foo(object, metaclass=upper_attr):
    # metaclass默认的参数是type
    bar = 'bip'

f = Foo()
print(f.BAR)

'''
可以通过对元类的修改(即通过修改type创建类传入的参数来实现),来定义一系列有特殊要求的类,比如指定继承的父类
'''
原文地址:https://www.cnblogs.com/kogmaw/p/12602595.html