Python3 元类编程

    技术2022-07-10  107

    在Python中一切接对象,类也是一个对象,所有的类都是有type类创建,我们实际开发中最常用的type方法,是用来获取某个对象的类型的,例如type(1) ⇒ int 、type(‘str’) ⇒ str。但是type还有一种用法,就是用来创建类的。

    1、通过type动态创建无父类、无属性的类
    People = type('People', (), {}) p = People() print(p)
    2、通过type动态创建有父类、无属性的类
    class Animal(object): name = '动物类' public = '类变量' def __init__(self, name): self.name = name Dog = type('Dog', (Animal, ), {}) d = Dog('哈士奇') print(d) print(Dog.__bases__) print(Dog.__mro__) print(d.name) print(d.public)
    3、通过type动态创建有父类、有属性的类
    class Animal(object): name = '动物类' public = '类变量' def __init__(self, name): self.name = name Dog = type('Dog', (Animal, ), {'age': 2}) # 新增一个age属性 d = Dog('哈士奇') print(d) print(Dog.__bases__) print(Dog.__mro__) print(d.name) print(d.public) print(d.age)

    值得注意的是:在类中方法也是一种属性,只不过该属性的值是函数地址而已,因此使用type创建类时,我们也可以创建类的一些方法。 例如:

    # 通过type动态创建有类,添加类方法、静态方法、实例方法 @classmethod def eat(cls): print('类方法') @staticmethod def say(): print('静态方法') def get_age(self): print('今年%d岁了' % self.age) People = type('People', (), {'age': 2, 'eat': eat, 'say': say, 'get_age': get_age}) p = People() People.eat() p.eat() p.say() People.say() p.get_age()
    4、在编写类时,更改元类,使用自建的元类来创建类

    __metaclass__属性,Python会在类的定义中寻找__metaclass__属性,如果找到了,Python就会用它来创建类,如果没有找到,就会用内建的type来创建这个类

    def upper_attr(cls, bases, attrs): ''' 自定义元类,将所有的共有属性名都变成大写的属性名 ''' newAttr = {} for key, val in attrs.items(): if not key.startswith('__'): newAttr[key.upper()] = val return type(cls, bases, newAttr) class People(object, metaclass=upper_attr): name = 'asd' age = 18 p = People() print(p.NAME) # p.name 会报属性找不到 print(p.AGE)
    Processed: 0.014, SQL: 9