类和对象-type
>>> # 创造类的模板,type就是一个元类,所有元类都继承自type>>> class MetaC(type):
... pass
...
>>> class C(metaclass=MetaC):
... pass
...
>>> c = C()
>>> print(type(c))
<class '__main__.C'>
>>> print(type(C))
<class '__main__.MetaC'>
>>> # 未指定元类的话是<class 'type'>
>>> print(type(MetaC))
<class 'type'>
>>> class MetaC(type):
... def __new__(mcls, name, bases, attrs):
... print('__new__() in MetaC~~')
... return type.__new__(mcls, name, bases, attrs)
... def __init__(cls, name, bases, attrs):
... print('__init__() in MetaC~~')
... type.__init__(cls, name, bases, attrs)
...
>>> class C(metaclass=MetaC):
... def __new__(cls):
... print('__new__() in C~~')
... return super().__new__(cls) # 调用的是object的__new__()
... def __init__(self):
... print('__init__() in C~~')
...
__new__() in MetaC~~
__init__() in MetaC~~
>>> # 元类里的__new__()方法是在类C定义完成的那一刻触发
>>> c = C()
__new__() in C~~
__init__() in C~~
>>> # object是所有类的父类,元类比类高一级别,元类继承自type
>>> # 元类两个方法中各参数的作用
>>> class MetaC(type):
... def __new__(mcls, name, bases, attrs):
... print(f'mcls={mcls}, name={name}, bases={bases}, attrs={attrs}')
... return type.__new__(mcls, name, bases,attrs)
... def __init__(cls, name, bases, attrs):
... type.__init__(cls, name, bases, attrs)
... print(f'cls={cls}, name={name}, bases={bases}, attrs={attrs}')
...
>>> class C(metaclass=MetaC):
... pass
...
mcls=<class '__main__.MetaC'>, name=C, bases=(), attrs={'__module__': '__main__', '__qualname__': 'C'}
cls=<class '__main__.C'>, name=C, bases=(), attrs={'__module__': '__main__', '__qualname__': 'C'}
>>> # 各参数作用跟上节type中各参数作用一致
>>> # 把__call__()方法定义在元类里,拦截类实例化对象的操作
>>> class MetaC(type):
... def __call__(cls, *args, **kwargs):
... print('__call__() in MetaC~~')
...
>>> class C(metaclass=MetaC):
... pass
...
>>> c = C()
__call__() in MetaC~~
>>> # 绝大多数元类用到的就是__new__()、__init__()、__call__()三个魔法方法
>>>
页:
[1]