python中metaclass原理與用法詳解
本文實(shí)例講述了python中metaclass原理與用法。分享給大家供大家參考,具體如下:
什么是 metaclass.
metaclass (元類)就是用來創(chuàng)建類的類。在前面一篇文章《python動(dòng)態(tài)創(chuàng)建類》里我們提到過,可以用如下的一個(gè)觀點(diǎn)來理解什么是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 是用來創(chuàng)建字符串的,int 是用來 創(chuàng)建整形對(duì)象,所以type 用來創(chuàng)建 class object的,都用小寫好了。
在python中的任何東西都是對(duì)象。包括int,str,function,class等。他們都是從一個(gè)class 創(chuàng)建的,我們可以通過查看 __class__ 屬性來檢查.
>>> 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'>
檢查__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, 感覺是不是讓你有點(diǎn)困惑呢。
python 將在你的class定義中查找__metaclass__,如果找到,就會(huì)用這個(gè)metaclass去創(chuàng)建Foo class,如果沒有找到,就會(huì)用 type 去創(chuàng)建class.如果上篇文章提到的一樣.所以,當(dāng)你
class Foo(Bar): pass
pyton 將會(huì)如下去解析:是否有__metaclass__ 在Foo 里面,如果是的,則用metaclass 去創(chuàng)建一個(gè)名字為 ”Foo" 的class object. 如果沒有找到,則看其基類Bar里面是否有__metaclass__,如果基類沒有,則看所在的module 層是否有__metaclass__,如果都沒有的話,則調(diào)用 type 去創(chuàng)建這個(gè)類。
現(xià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è)類來處理
# 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)
通過上面的例子可以了解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中requests庫(kù)session對(duì)象的妙用詳解
這篇文章主要介紹了python中requests庫(kù)session對(duì)象的妙用詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
如何利用Python實(shí)現(xiàn)自動(dòng)打卡簽到的實(shí)踐
簽到,都是規(guī)律性的操作,何嘗不寫一個(gè)程序加到Windows實(shí)現(xiàn)自動(dòng)簽到呢,本文就主要介紹了如何利用Python實(shí)現(xiàn)自動(dòng)打卡簽到的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12
python sorted函數(shù)原理解析及練習(xí)
這篇文章主要介紹了python sorted函數(shù)原理解析及練習(xí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
pandas.DataFrame寫入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式
這篇文章主要介紹了pandas.DataFrame寫入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于Python 解決Python3.9 pandas.read_excel(‘xxx.xlsx‘)報(bào)錯(cuò)的問題
這篇文章主要介紹了關(guān)于Python 解決Python3.9 pandas.read_excel(‘xxx.xlsx‘)報(bào)錯(cuò)的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
keras 模型參數(shù),模型保存,中間結(jié)果輸出操作
這篇文章主要介紹了keras 模型參數(shù),模型保存,中間結(jié)果輸出操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
使用Python對(duì)零售商品進(jìn)行數(shù)據(jù)分析
這篇文章主要為大家介紹了使用Python對(duì)零售商品進(jìn)行數(shù)據(jù)分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的線程池
這篇文章主要介紹了用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的線程池,通過這個(gè)小程序可以幫助更好地理解Python中線程的運(yùn)行機(jī)制,需要的朋友可以參考下2015-04-04

