metaclass(元类)

时间:2021-09-24 21:56:52

一、创建类的执行流程

metaclass(元类)

metaclass(元类)

二、元类的认识

什么是元类呢?在Python3中继承type的就是元类

二、元类的示例

方式一:

# 方式一
class MyType(type):
'''继承type的就是元类'''
def __init__(self,*args,**kwargs):
print("MyType创建的对象",self) #Foo
super(MyType,self).__init__(*args,**kwargs) def __call__(self, *args, **kwargs):
obj = super(MyType,self).__call__(*args,**kwargs)
print("类创建对象",self,obj) #Foo class Foo(object,metaclass=MyType): # 对象加括号会去执行__call__方法,__call__方法里面继承了type的__call__方法
,type的__call__方法里面会先执行__new__方法,再去执行__init__方法。
所以,Foo就是用type创建出来的
user = "haiyan"
age = 18 obj = Foo()

方式二

# 方式二
class MyType(type):
def __init__(self, *args, **kwargs):
print("ssss")
super(MyType, self).__init__(*args, **kwargs) def __call__(cls, *args, **kwargs):
v = dir(cls)
obj = super(MyType, cls).__call__(*args, **kwargs)
return obj
#对象加括号就会去执行__call__方法
class Foo(MyType('Zcc', (object,), {})): #MyType('Zcc', (object,), {})相当于class Zcc(object):pass,也就是创建了一个Zcc的类
user = 'haiyan'
age = 18 obj = Foo()

方式三

# 方式三
class MyType(type):
def __init__(self, *args, **kwargs):
print("ssss")
super(MyType, self).__init__(*args, **kwargs) def __call__(cls, *args, **kwargs):
v = dir(cls)
obj = super(MyType, cls).__call__(*args, **kwargs)
return obj
#对象加括号就会去执行__call__方法 def with_metaclass(arg,base):
print("类对象",MyType('Zcc', (base,), {}))
return arg('Zcc', (base,), {}) #返回一个类对象 <class '__main__.Zcc'> class Foo(with_metaclass(MyType,object)): #MyType('Zcc', (object,), {})相当于class Zcc(object):pass,也就是创建了一个Zcc的类
user = 'haiyan'
age = 18 obj = Foo()

=======================附加===========================

 class ASD(type):
pass qqq = ASD("qwe", (object,), {}) #用ASD这个元类创建了一个(qwe,并且继承object类的)类 # class ASD(qwe):
# pass
obj = qqq()
# 能创建类的是元类
# 能创建对象的是类
print(obj) #<__main__.qwe object at 0x00000000024FFBA8>
print(obj.__class__) #<class '__main__.qwe'>
print(obj.__class__.__class__) #<class '__main__.ASD'>
print(obj.__class__.__class__.__class__) #<class 'type'>
print(obj.__class__.__class__.__class__.__class__) #<class 'type'>