鱼C论坛

 找回密码
 立即注册
查看: 2076|回复: 0

[学习笔记] 类和对象-type

[复制链接]
发表于 2023-4-4 22:06:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
>>> # 创造类的模板,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__()三个魔法方法
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-24 01:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表