python中metaclass原理與用法詳解
本文實(shí)例講述了python中metaclass原理與用法。分享給大家供大家參考,具體如下:
什么是 metaclass.
metaclass (元類)就是用來(lái)創(chuàng)建類的類。在前面一篇文章《python動(dòng)態(tài)創(chuàng)建類》里我們提到過(guò),可以用如下的一個(gè)觀點(diǎn)來(lái)理解什么是metaclass:
MyClass = MetaClass() MyObject = MyClass()
metaclass是python 里面的編程魔法
同時(shí)在前面一篇《python動(dòng)態(tài)創(chuàng)建類》文章里描述動(dòng)態(tài)創(chuàng)建class 的時(shí)候介紹了type,他允許你用如下的方法創(chuàng)建一個(gè)類:
MyClass = type('MyClass', (), {})
其根本原因就在于 type 就是一個(gè) metaclass, python利用type在后面創(chuàng)建各種各樣的類。搞不明白的是,為什么是 "type" 而不是 "Type",可能考慮到 str 是用來(lái)創(chuàng)建字符串的,int 是用來(lái) 創(chuàng)建整形對(duì)象,所以type 用來(lái)創(chuàng)建 class object的,都用小寫好了。
在python中的任何東西都是對(duì)象。包括int,str,function,class等。他們都是從一個(gè)class 創(chuàng)建的,我們可以通過(guò)查看 __class__ 屬性來(lái)檢查.
>>> age = 35 >>> age.__class__ <type 'int'> >>> name = 'bob' >>> name.__class__ <type 'str'> >>> def foo(): pass >>> foo.__class__ <type 'function'> >>> class Bar(object): pass >>> b = Bar() >>> b.__class__ <class '__main__.Bar'>
檢查_(kāi)_class__屬性
>>> a.__class__.__class__ <type 'type'> >>> age.__class__.__class__ <type 'type'> >>> foo.__class__.__class__ <type 'type'> >>> b.__class__.__class__ <type 'type'>
發(fā)現(xiàn)什么了,結(jié)果都是 "type", 其實(shí) type 就是python內(nèi)置的一個(gè)metaclass.當(dāng)然,你可以創(chuàng)建自己的metaclass. 這里有一個(gè)很重要的屬性:
__metaclass__ 屬性
當(dāng)你在寫一個(gè)class的時(shí)候,你可以加入__metaclass__屬性.
class Foo(object): __metaclass__ = something... [...]
如果你這么做了,那么python 將調(diào)用 metaclass 去創(chuàng)建 Foo class, 感覺(jué)是不是讓你有點(diǎn)困惑呢。
python 將在你的class定義中查找__metaclass__,如果找到,就會(huì)用這個(gè)metaclass去創(chuàng)建Foo class,如果沒(méi)有找到,就會(huì)用 type 去創(chuàng)建class.如果上篇文章提到的一樣.所以,當(dāng)你
class Foo(Bar): pass
pyton 將會(huì)如下去解析:是否有__metaclass__ 在Foo 里面,如果是的,則用metaclass 去創(chuàng)建一個(gè)名字為 ”Foo" 的class object. 如果沒(méi)有找到,則看其基類Bar里面是否有__metaclass__,如果基類沒(méi)有,則看所在的module 層是否有__metaclass__,如果都沒(méi)有的話,則調(diào)用 type 去創(chuàng)建這個(gè)類。
現(xiàn)在的問(wèn)題是,__metaclass__ 里面到底能做什么?結(jié)論是:能創(chuàng)建一個(gè)class的東西。什么能創(chuàng)建一個(gè)class, 其實(shí)就是 type,或者type 的子類(subclass)。
自定義 metaclass
metaclass的主要目的就是在創(chuàng)建類的時(shí)候,做一些自動(dòng)的改變。比如,打個(gè)不恰當(dāng)?shù)谋确?,我們打算將一個(gè)module里所有類的屬性都變成大寫的。其中一種處理辦法就是用 __metaclass__(申明在module上).
我們打算利用 metaclass 把所有的屬性變成大寫的。__metaclass__并不一定要求是一個(gè)class, 是一個(gè)可以調(diào)用的方法也是可以的。我們就從一個(gè)簡(jiǎn)單的例子看起
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'
現(xiàn)在用一個(gè)類來(lái)處理
# 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的做法,直接調(diào)用了type方法,而不是調(diào)用父類的__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 ,這其實(shí)就相于self,普通類方法里的self.一個(gè)更通用的方法如下:
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)
通過(guò)上面的例子可以了解metaclass了,也了解了在__init__方法,__new__方法里去做一個(gè)hook.當(dāng)然還可以在__call__里面做文章,但更多的人喜歡在__init__里面修改 。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python?中?Selenium?的?send_keys()?函數(shù)用法小結(jié)
send_keys() 是將數(shù)字、文本和符號(hào)等鍵盤輸入發(fā)送到應(yīng)用程序的文本框的過(guò)程, send_keys() 是 WebDriver 的一部分,每個(gè)鍵盤輸入都會(huì)發(fā)送到此元素,這篇文章主要介紹了Python?中?Selenium?的?send_keys()?函數(shù),需要的朋友可以參考下2023-11-11
Python常用隨機(jī)數(shù)與隨機(jī)字符串方法實(shí)例
這篇文章主要介紹了Python常用隨機(jī)數(shù)與隨機(jī)字符串方法實(shí)例,本文講解了隨機(jī)整數(shù)、隨機(jī)選取0到100間的偶數(shù)、隨機(jī)浮點(diǎn)數(shù)、隨機(jī)字符串等常用隨機(jī)方法,需要的朋友可以參考下2015-04-04
python3+PyQt5 使用三種不同的簡(jiǎn)便項(xiàng)窗口部件顯示數(shù)據(jù)的方法
今天小編就為大家分享一篇python3+PyQt5 使用三種不同的簡(jiǎn)便項(xiàng)窗口部件顯示數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python實(shí)時(shí)監(jiān)控網(wǎng)站瀏覽記錄實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Python實(shí)時(shí)監(jiān)控網(wǎng)站瀏覽記錄實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
python狀態(tài)機(jī)transitions庫(kù)詳解
在用python做一個(gè)比較復(fù)雜的小項(xiàng)目,需要根據(jù)不同的輸入,控制攝像頭采集執(zhí)行不同的任務(wù).雖然用流程方式實(shí)現(xiàn)了,但閱讀起來(lái)費(fèi)勁,還容易出錯(cuò).所以就用了狀態(tài)機(jī),需要的朋友可以參考下2021-06-06
python 實(shí)現(xiàn)多線程下載視頻的代碼
這篇文章主要介紹了python 實(shí)現(xiàn)多線程下載視頻的代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
python3.8與pyinstaller沖突問(wèn)題的快速解決方法
這篇文章主要介紹了python3.8與pyinstaller沖突問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python學(xué)習(xí)小技巧之列表項(xiàng)的排序
這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)排序的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們可以參借鑒,下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-05-05
Python自動(dòng)安裝第三方庫(kù)的小技巧(pip使用詳解)
很多朋友私信小編Python安裝第三方庫(kù)安裝技巧,在這就不一一回復(fù)大家了,今天小編給大家分享一篇教程關(guān)于Python自動(dòng)安裝第三方庫(kù)的小技巧,本文以安裝plotly為例給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2021-05-05

