本文實例講述了python中metaclass原理與用法。分享給大家供大家參考,具體如下:
什么是 metaclass.
metaclass (元類)就是用來創建類的類。在前面一篇文章《python動態創建類》里我們提到過,可以用如下的一個觀點來理解什么是metaclass:
MyClass = MetaClass() MyObject = MyClass()
metaclass是python 里面的編程魔法
同時在前面一篇《python動態創建類》文章里描述動態創建class 的時候介紹了type,他允許你用如下的方法創建一個類:
MyClass = type('MyClass', (), {})
其根本原因就在于 type 就是一個 metaclass, python利用type在后面創建各種各樣的類。搞不明白的是,為什么是 "type" 而不是 "Type",可能考慮到 str 是用來創建字符串的,int 是用來 創建整形對象,所以type 用來創建 class object的,都用小寫好了。
在python中的任何東西都是對象。包括int,str,function,class等。他們都是從一個class? 創建的,我們可以通過查看 __class__ 屬性來檢查.
>>> age = 35 >>> age.__class__>>> name = 'bob' >>> name.__class__ >>> def foo(): pass >>> foo.__class__ >>> class Bar(object): pass >>> b = Bar() >>> b.__class__
檢查__class__屬性
>>> a.__class__.__class__>>> age.__class__.__class__ >>> foo.__class__.__class__ >>> b.__class__.__class__
發現什么了,結果都是 "type", 其實? type 就是python內置的一個metaclass.當然,你可以創建自己的metaclass. 這里有一個很重要的屬性:
__metaclass__ 屬性
當你在寫一個class的時候,你可以加入__metaclass__屬性.
class Foo(object): __metaclass__ = something... [...]
如果你這么做了,那么python 將調用 metaclass 去創建 Foo class, 感覺是不是讓你有點困惑呢。
python 將在你的class定義中查找__metaclass__,如果找到,就會用這個metaclass去創建Foo class,如果沒有找到,就會用 type 去創建class.如果上篇文章提到的一樣.所以,當你
class Foo(Bar): pass
pyton 將會如下去解析:是否有__metaclass__ 在Foo 里面,如果是的,則用metaclass? 去創建一個名字為 ”Foo" 的class object. 如果沒有找到,則看其基類Bar里面是否有__metaclass__,如果基類沒有,則看所在的module 層是否有__metaclass__,如果都沒有的話,則調用 type 去創建這個類。
現在的問題是,__metaclass__ 里面到底能做什么?結論是:能創建一個class的東西。什么能創建一個class, 其實就是 type,或者type 的子類(subclass)。
自定義 metaclass
metaclass的主要目的就是在創建類的時候,做一些自動的改變。比如,打個不恰當的比方,我們打算將一個module里所有類的屬性都變成大寫的。其中一種處理辦法就是用 __metaclass__(申明在module上).
我們打算利用 metaclass 把所有的屬性變成大寫的。__metaclass__并不一定要求是一個class, 是一個可以調用的方法也是可以的。我們就從一個簡單的例子看起
def upper_attr(future_class_name, future_class_parents, future_class_attr): """ Return a class object, with the list of its attribute turned into uppercase. """ # pick up any attribute that doesn't start with '__' attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__')) # turn them into uppercase uppercase_attr = dict((name.upper(), value) for name, value in attrs) # let `type` do the class creation return type(future_class_name, future_class_parents, uppercase_attr) __metaclass__ = upper_attr # this will affect all classes in the module class Foo(): # global __metaclass__ won't work with "object" though # but we can define __metaclass__ here instead to affect only this class # and this will work with "object" childrend bar = 'bip' print hasattr(Foo, 'bar') # Out: False print hasattr(Foo, 'BAR') # Out: True f = Foo() print f.BAR # Out: 'bip'
現在用一個類來處理
# remember that `type` is actually a class like `str` and `int` # so you can inherit from it class UpperAttrMetaclass(type): # __new__ is the method called before __init__ # it's the method that creates the object and returns it # while __init__ just initializes the object passed as parameter # you rarely use __new__, except when you want to control how the object # is created. # here the created object is the class, and we want to customize it # so we override __new__ # you can do some stuff in __init__ too if you wish # some advanced use involves overriding __call__ as well, but we won't # see this def __new__(upperattr_metaclass, future_class_name, future_class_parents, future_class_attr): attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) return type(future_class_name, future_class_parents, uppercase_attr)
顯然這不是很oop的做法,直接調用了
type
方法,而不是調用父類的
__new__
方法,下面這么做:
class UpperAttrMetaclass(type): def __new__(upperattr_metaclass, future_class_name, future_class_parents, future_class_attr): attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) # reuse the type.__new__ method # this is basic OOP, nothing magic in there return type.__new__(upperattr_metaclass, future_class_name, future_class_parents, uppercase_attr)
你可能注意到 upperattr_metaclass ,這其實就相于
self
,普通類方法里的self.一個更通用的方法如下:
class UpperAttrMetaclass(type): def __new__(cls, name, bases, dct): attrs = ((name, value) for name, value in dct.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) return super(UpperAttrMetaclass, cls).__new__(cls, name, bases, uppercase_attr)
通過上面的例子可以了解metaclass了,也了解了在
__init__
方法,
__new__
方法里去做一個hook.當然還可以在
__call__
里面做文章,但更多的人喜歡在
__init__
里面修改 。
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
