metaclass(元類)
阿新 • • 發佈:2018-12-19
meta *args all png __main__ return def asd {}
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()
=======================附加===========================
1 class ASD(type):
2 pass
3
4 qqq = ASD("qwe", (object,), {}) #用ASD這個元類創建了一個(qwe,並且繼承object類的)類
5
6 # class ASD(qwe):
7 # pass
8 obj = qqq()
9 # 能創建類的是元類
10 # 能創建對象的是類
11 print(obj) #<__main__.qwe object at 0x00000000024FFBA8>
12 print(obj.__class__) #<class ‘__main__.qwe‘>
13 print(obj.__class__.__class__) #<class ‘__main__.ASD‘>
14 print(obj.__class__.__class__.__class__) #<class ‘type‘>
15 print(obj.__class__.__class__.__class__.__class__) #<class ‘type‘>
metaclass(元類)