元編程,一個聽起來特別酷的詞,強大的Lisp在這方面是好手,對于Python,盡管沒有完善的元編程范式,一些天才的開發(fā)者還是創(chuàng)作了很多元編程的魔法。Django的ORM就是元編程的一個很好的例子。
本篇的概念和例子皆在Python3.6環(huán)境下
一切都是對象
Python里一切都是對象(object
),基本數(shù)據(jù)類型,如數(shù)字,字串,函數(shù)都是對象。對象可以由類(class
)進行創(chuàng)建。既然一切都是對象,那么類是對象嗎?
是的,類也是對象,那么又是誰創(chuàng)造了類呢?答案也很簡單,也是類,一個能創(chuàng)作類的類,就像上帝一樣,開啟了萬物之始。這樣的類,稱之為元類(classmeta
)。
類的定義
對象是通過類創(chuàng)建的,這個很好理解。例如下面的代碼:
1
2
3
4
5
6
|
class Bar( object ): pass bar = Bar() print (bar, bar.__class__) # <__main__.Bar object at 0x101eb4630> <class '__main__.Bar'> print (Bar, Bar.__class__) # <class '__main__.Bar'> <class 'type'> |
可以看見對象 bar 是類 Bar 創(chuàng)建的實例。然而 Bar,看起來卻是由一個叫 type 的類創(chuàng)建的實例。即 bar <-- Bar < -- type
。
上面的例子,對象是動態(tài)創(chuàng)建的,類則是通過關(guān)鍵字 class 聲明定義的。class關(guān)鍵字背后的玄機是什么呢?
實際上,class Bar(object)
這樣的代碼,等價于 Bar = type('Bar', (objects, ), {})
即類 type 通過實例化創(chuàng)建了它的對象 Bar,而這個 Bar 恰恰是一個類。這樣能創(chuàng)建類的類,就是 Python 的元類。
從創(chuàng)建 Bar 的代碼上來看,元類 type 的 __init__
方法有3個參數(shù),
- 第一個是創(chuàng)建的類的名字
- 第二個是其繼承父類的元類列表,
- 最后就是一個屬性字典,即該類所具有的屬性。
type 元類
type是小寫,因而很容易誤以為它是一個函數(shù)。通過help(type)可以看到它的定義如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class type ( object ): """ type(object_or_name, bases, dict) type(object) -> the object's type type(name, bases, dict) -> a new type """ def __init__( cls , what, bases = None , dict = None ): # known special case of type.__init__ """ type(object_or_name, bases, dict) type(object) -> the object's type type(name, bases, dict) -> a new type # (copied from class doc) """ pass @staticmethod # known case of __new__ def __new__( * args, * * kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass |
如前所述,__init__
方法接受三個參數(shù),type 實例化的過程,會創(chuàng)建一個新的類。創(chuàng)建類的代碼來自 __new__
方法,它的參數(shù)其實和 __init__
,一樣。至于它們之間有什么關(guān)系,后面再做介紹。目前只要知道,當調(diào)用 type 進行實例化的時候,會先自動調(diào)用 __new__
方法,然后再接著調(diào)用 __init__
方法,在類外面來看,最終會實例化一個對象,這個對象是一個類。
從 type 的定義來看,它繼承 object,Python3的所有類,都繼承來著 object,類type 也是 object 的實例,令人奇怪的是,object 既是類也是對象,它也是由 type實例化而來。有一種雞生蛋,蛋生雞的悖論。暫且先不管,只要知道所有類的頂級繼承來自 object 就好。
自定義元類
既然元類可以創(chuàng)建類,那么自定義元類就很簡單了,直接繼承類 type 即可。先看下面一個例子:
1
2
3
4
5
6
7
8
9
10
|
class MyType( type ): pass pass print (MyType, MyType.__class__) # <class '__main__.MyType'> <class 'type'> print (Bar, Bar.__class__) # <class '__main__.Bar'> <class '__main__.MyType'> |
可以看到,Bar在聲明的時候,指定了其元類,此時的類 Bar 的__class__
屬性不再是 type,而是 MyType。即之前定義 Bar 的代碼不再是 Bar = type('Bar', (objects, ), {})
, 而是 Bar = MyType('Bar', (objects, ), {})
。創(chuàng)建的元類的代碼是MyType = type('MyType', (objects, ), {})
。
如果一個類沒有顯示的指定其元類,那么會沿著繼承鏈尋找父類的元類,如果一直找不到,那么就使用默認的 type 元類。
元類沖突
每個類都可以指定元類,但是父類和子類的元類要是一條繼承關(guān)系上的,否則會出現(xiàn)元類沖突。并且這個繼承關(guān)系中,以繼承最后面的元類為其元類。
元類的查找順序大致為,先查看其繼承的父類,找到父類的元類即停止。若直接父類沒有元類,直到頂級父類 object ,此時父類(object)的元類是 type(basemetaclass),再看其自身有沒有指定元類(submetaclass),如果指定了元類(submetaclass),再對比這個子元類(submetaclass)和父元類(basemetaclass),如果它們毫無繼承關(guān)系,那么將會拋出元類沖突的錯誤。如果指定的子元類是父元類的父類,那么將會使用父元類,否則將使用期指定的子元類。
即 submetaclass <- basemetaclass
使用 submetaclass
作為最終元類,
若 basemetaclass <- submetaclass
, 使用 basemetaclass
作為最終元類,
兩者無繼承關(guān)系,拋出沖突。
有點像繞口令,且看代碼例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class MyType( type ): pass # 等價于 MyType = type('MyType', (object, ), {}) class Bar( object , metaclass = MyType): pass # 等價于 Bar = MyType('Bar', (object, ), {}) class Foo(Bar): pass # 等價于 Foo = MyType('Foo', (Foo, object, ), {}) print (Bar, Bar.__class__) # <class '__main__.Bar'> <class '__main__.MyType'> print (Foo, Foo.__class__) # <class '__main__.Foo'> <class '__main__.MyType'> |
Bar的父元類(basemetaclass)type,指定子元類(submetaclass)是 MyType, MyType 繼承自 type,所以Bar的元類是 MyType。
又如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class MyType( type ): pass class Bar( object , metaclass = MyType): pass class Foo(Bar, metaclass = type ): pass print (Bar, Bar.__class__) # <class '__main__.Bar'> <class '__main__.MyType'> print (Foo, Foo.__class__) # <class '__main__.Foo'> <class '__main__.MyType'> |
盡管 Foo 也指定了元類(submetaclass) type,可是其父類的元類(basemetaclass)是 MyType, MyType
是 type的子類,因此 Foo的元類拋棄了指定的(submetaclass) type,而是沿用了其父類的MyType。
當 submetaclass 和 basemetaclass 沒有繼承關(guān)系的時候,將會元類沖突
1
2
3
4
5
6
7
8
9
10
11
12
|
class MyType( type ): pass class MyOtherType( type ): pass class Bar( object , metaclass = MyType): pass class Foo(Bar, metaclass = MyOtherType): pass |
運行代碼,當定義的時候就會出現(xiàn)TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict)
元類沖突的錯誤。
修改代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class MyType( type ): pass class MyOtherType(MyType): pass class Bar( object , metaclass = MyType): pass class Foo(Bar, metaclass = MyOtherType): pass print (Bar, Bar.__class__) # <class '__main__.Bar'> <class '__main__.MyType'> print (Foo, Foo.__class__) # <class '__main__.Foo'> <class '__main__.MyOtherType'> |
可以看到 Bar 和 Foo 分別有自己的元類,并且都符合繼承關(guān)系中尋找。再調(diào)換一下元類看看:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class MyType( type ): pass class MyOtherType(MyType): pass class Bar( object , metaclass = MyOtherType): pass class Foo(Bar, metaclass = MyType): pass print (Bar, Bar.__class__) # <class '__main__.Bar'> <class '__main__.MyOtherType'> print (Foo, Foo.__class__) # <class '__main__.Foo'> <class '__main__.MyOtherType'> |
都使用了Foo還是使用了元子類作為元類。究其原因,其實也很好理解。定義父類的時候,使用了元類MyOtherType 。定義子類的時候,通過繼承,找到了創(chuàng)建父類的元類,那么父類就是 MyOtherType 的實例。
如果使用 MyType 做為元類,那么他就是 MyType 的實例,MyType的實例會比MyOtherType具有的屬性少,那么在繼承鏈上,它又是 Bar的子類,這樣看就是子類比父類還狹窄了,顯然不是一個好的關(guān)系。即變成了下面的關(guān)系
Bar <- MyOtherType
| ↑
| |
↓ |Foo <- MyType
因此當 MyType 是 MyOtherType的父類的時候,即使 Foo 指定了 MyType作為元類,還是會被忽略,使用其父元類MyOtherType。
上面的線的箭頭要一直,才能使用各自指定的元類,否則使用箭頭指向的那個類作為元類。元類沒有繼承關(guān)系,元類沖突。
對象(類)實例化
目前為止,我們了解了類的定義,即類是如何被元類創(chuàng)建出來的,但是創(chuàng)建的細節(jié)尚未涉及。即元類是如何通過實例化創(chuàng)建類的過程。這也是對象創(chuàng)建的過程。
前文介紹了一個對象是通過類創(chuàng)建的,類對象是通過元類創(chuàng)建的。創(chuàng)建類中,會先調(diào)用元類的__new__方法,設(shè)置其名稱,繼承關(guān)系和屬性,返回一個實例。然后再調(diào)用實例的__init__方法進行初始化實例對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class MyType( type ): def __init__( self , * args, * * kwargs): print ( 'init ' , id ( self ), args, kwargs) def __new__( cls , * args, * * kwargs): print ( 'new' , id ( cls ), args, kwargs) instance = super (MyType, cls ).__new__( cls , * args, * * kwargs) print ( id (instance)) return instance class Bar( object , metaclass = MyType): pass |
運行代碼可以看見輸出:
new 4323381304 ('Bar', (<class 'object'>,), {'__module__': '__main__', '__qualname__': 'Bar'}) {}
4323382232
init 4323382232 ('Bar', (<class 'object'>,), {'__module__': '__main__', '__qualname__': 'Bar'}) {}
注意,上面代碼僅關(guān)注 Bar
類的創(chuàng)建,即 Bar =MyType('Bar', (object, ), {})
這個定義代碼。MyType進行實例化創(chuàng)建 Bar的過程中,會先用 其 __new__
方法,后者調(diào)用了父類 type的 __new__
方法,并返回了元類的實例, 同時調(diào)用這個實例的__init__
方法,后者對改實例對象進行初始化。這也就是為什么方法名為 __init__
。
通常我們會在 __init__
方法初始化一些實例對象的屬性如果 __new__
方法什么也不返回,那么 __init__
方法是不會被調(diào)用的。
instance = super(MyType, cls).__new__(cls, *args, **kwargs)
, 有的地方也喜歡寫成type.__new__
或者type
,前者是python中如何調(diào)用父類方法的問題,后者是直接使用type創(chuàng)建類的過程。比較推薦的寫法還是使用 super 調(diào)用其父類的方法的方式。
類是元類的對象,普通類創(chuàng)建對象的過程,也是一樣。因此,只要重寫 __new__方法,還可以實現(xiàn)一個類還可以創(chuàng)建另外一個類的實例的魔法。
移花接木
重寫 __new__
方法,讓其創(chuàng)建另外一個類的實例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class Bar: def __init__( self , name): self .name = name print ( 'Bar init' ) def say( self ): print ( 'say: Bar {}' . format ( self .name)) class Foo( object ): def __init__( self ): print ( 'self {}' . format ( self )) def __new__( cls , * args, * * kwargs): instance = super (Foo, cls ).__new__(Bar, * args, * * kwargs) print ( 'instance {}' . format (instance)) instance.__init__( 'a class' ) return instance def say( self ): print ( 'say: Foo' ) m = Foo() print ( 'm {}' . format (m)) m.say() |
輸出
instance <__main__.Bar object at 0x104033240>
Bar init
m <__main__.Bar object at 0x104033240>
say: Bar a class
在類 Foo 中,通過重寫 __new__返回了一個 Bar 類的實例對象,然后調(diào)用 Bar 實例的 __inti__ 方法初始化,由于返回了 bar 實例,因此 Foo 的實例沒有被創(chuàng)建,因此也不會調(diào)用它的實例方法 __inti__ 。這樣就把 移花(Bar)接木(Foo)上了。
也許有人會覺得這樣的詭異魔法有什么用呢?實際上,Tornado框架使用了這樣的技術(shù)實現(xiàn)了一個叫 Configurable 的工廠類,用于創(chuàng)建不同網(wǎng)絡(luò)IO下的epoll還是select模型。有興趣可以參考其實現(xiàn)方式。
元類的應用
討論了那么多原理的東西,最后肯定是要應用到實際中才有意義。既然類可以被動態(tài)的創(chuàng)建,那么很多定義在類的方法,豈不是也可以被動態(tài)的創(chuàng)建了呢。這樣就省去了很多重復工作,也能實現(xiàn)酷酷的元編程。
元類可以創(chuàng)建單例模式,也可以用來實現(xiàn) ORM,下面介紹的是Django使用元類實現(xiàn)的查找方式。更經(jīng)典的model定義網(wǎng)上有很多例子,就不再介紹了。下面介紹一個model通過manger管理器實現(xiàn)查詢方法的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import inspect class QuerySet: def get( self , * args, * * kwargs): print ( 'get method' ) return self def filter ( self , * args, * * kwargs): print ( 'filter method' ) return self class BaseManager: def __init__( self ): pass @classmethod def from_queryset( cls , queryset_class, class_name = None ): if class_name is None : class_name = '%sFrom%s' % ( cls .__name__, queryset_class.__name__) class_dict = { '_queryset_class' : queryset_class, } class_dict.update( cls ._get_queryset_methods(queryset_class)) return type (class_name, ( cls ,), class_dict) def get_queryset( self ): return self ._queryset_class() @classmethod def _get_queryset_methods( cls , queryset_class): def create_method(name, method): def manager_method( self , * args, * * kwargs): return getattr ( self .get_queryset(), name)( * args, * * kwargs) manager_method.__name__ = method.__name__ manager_method.__doc__ = method.__doc__ return manager_method new_methods = {} for name, method in inspect.getmembers(queryset_class, predicate = inspect.isfunction): if hasattr ( cls , name): continue queryset_only = getattr (method, 'queryset_only' , None ) if queryset_only or (queryset_only is None and name.startswith( '_' )): continue new_methods[name] = create_method(name, method) return new_methods class Manager(BaseManager.from_queryset(QuerySet)): pass class ModelMetaClass( type ): def __new__( cls , * args, * * kwargs): name, bases, attrs = args attrs[ 'objects' ] = Manager() return super (ModelMetaClass, cls ).__new__( cls , name, bases, attrs) class Model( object , metaclass = ModelMetaClass): pass class User(Model): pass User.objects.get() User.objects. filter () User.objects. filter ().get() |
這樣model就用使用期管理器Manger 下的方法了。通過model的元類ModelMetaClass,定義model的時候,就初始化了一個 Manger對象掛載到Model下面,而定義Manger的時候,也通過元類將QuerySet下的查詢方法掛載到Manger下了。
總結(jié)
Python里一切都是對象,對象都是由類進行創(chuàng)建實例化而來。既然一切是對象,那么類也是對象,而類這種對象又是由一種更高級類創(chuàng)建而來,即所謂的元類。
元類可以創(chuàng)建類,Python默認的元類是 type。通過繼承type,可以自定義元類,在自定義元類的時候定義或者重載 __new__
,可以創(chuàng)建該類的實例對象,同時也可以修改類創(chuàng)建對象的行為。類通過 __new__
創(chuàng)建實例對象,然后調(diào)用實例對象的 __init__
初始化實例對象。
在使用自定義元類的時候,子類的的元類和父類的元類有關(guān)系,前者指定的元類必須和父類的元類是一個繼承關(guān)系上的,否則會出現(xiàn)元類沖突。子類選取元類的取決于指定的元類和父元類的繼承關(guān)系,子元類若是父元類的子類,則指定的元類為子元類,否則將會被忽略,使用父元類為其元類。
元類是元編程的一種技術(shù)手段,常用于實現(xiàn)工廠模式的策略。通過定義元類動態(tài)創(chuàng)建類和展開,可以實現(xiàn)很多設(shè)計精妙的應用。ORM 正式其中一種常用的方法。
以上就是詳解python metaclass(元類)的詳細內(nèi)容,更多關(guān)于python metaclass(元類)的資料請關(guān)注服務器之家其它相關(guān)文章!
原文鏈接:https://www.jianshu.com/p/b0f4d9c9afbb