本文實(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__
>>> 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__
發(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ì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

