最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中metaclass原理與用法詳解

 更新時間:2019年06月25日 10:40:32   作者:輕舞肥羊  
這篇文章主要介紹了python中metaclass原理與用法,結(jié)合具體實(shí)例形式分析了Python中metaclass的功能、原理及使用metaclass動態(tài)創(chuàng)建類相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python中metaclass原理與用法。分享給大家供大家參考,具體如下:

什么是 metaclass.

metaclass (元類)就是用來創(chuàng)建類的類。在前面一篇文章《python動態(tài)創(chuàng)建類》里我們提到過,可以用如下的一個觀點(diǎn)來理解什么是metaclass:

MyClass = MetaClass()
MyObject = MyClass()

metaclass是python 里面的編程魔法

同時在前面一篇《python動態(tài)創(chuàng)建類》文章里描述動態(tài)創(chuàng)建class 的時候介紹了type,他允許你用如下的方法創(chuàng)建一個類:

MyClass = type('MyClass', (), {})

其根本原因就在于 type 就是一個 metaclass, python利用type在后面創(chuàng)建各種各樣的類。搞不明白的是,為什么是 "type" 而不是 "Type",可能考慮到 str 是用來創(chuàng)建字符串的,int 是用來 創(chuàng)建整形對象,所以type 用來創(chuàng)建 class object的,都用小寫好了。

在python中的任何東西都是對象。包括int,str,function,class等。他們都是從一個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)置的一個metaclass.當(dāng)然,你可以創(chuàng)建自己的metaclass. 這里有一個很重要的屬性:

__metaclass__ 屬性

當(dāng)你在寫一個class的時候,你可以加入__metaclass__屬性.

class Foo(object):
 __metaclass__ = something...
 [...]

如果你這么做了,那么python 將調(diào)用 metaclass 去創(chuàng)建 Foo class, 感覺是不是讓你有點(diǎn)困惑呢。

python 將在你的class定義中查找__metaclass__,如果找到,就會用這個metaclass去創(chuàng)建Foo class,如果沒有找到,就會用 type 去創(chuàng)建class.如果上篇文章提到的一樣.所以,當(dāng)你

class Foo(Bar):
 pass

pyton 將會如下去解析:是否有__metaclass__ 在Foo 里面,如果是的,則用metaclass  去創(chuàng)建一個名字為 ”Foo" 的class object. 如果沒有找到,則看其基類Bar里面是否有__metaclass__,如果基類沒有,則看所在的module 層是否有__metaclass__,如果都沒有的話,則調(diào)用 type 去創(chuàng)建這個類。

現(xiàn)在的問題是,__metaclass__ 里面到底能做什么?結(jié)論是:能創(chuàng)建一個class的東西。什么能創(chuàng)建一個class, 其實(shí)就是 type,或者type 的子類(subclass)。

自定義 metaclass

metaclass的主要目的就是在創(chuàng)建類的時候,做一些自動的改變。比如,打個不恰當(dāng)?shù)谋确剑覀兇蛩銓⒁粋€module里所有類的屬性都變成大寫的。其中一種處理辦法就是用 __metaclass__(申明在module上).

我們打算利用 metaclass 把所有的屬性變成大寫的。__metaclass__并不一定要求是一個class, 是一個可以調(diào)用的方法也是可以的。我們就從一個簡單的例子看起

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)在用一個類來處理

# 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.一個更通用的方法如下:

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.當(dāng)然還可以在__call__里面做文章,但更多的人喜歡在__init__里面修改 。

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • Python?中?Selenium?的?send_keys()?函數(shù)用法小結(jié)

    Python?中?Selenium?的?send_keys()?函數(shù)用法小結(jié)

    send_keys() 是將數(shù)字、文本和符號等鍵盤輸入發(fā)送到應(yīng)用程序的文本框的過程, send_keys() 是 WebDriver 的一部分,每個鍵盤輸入都會發(fā)送到此元素,這篇文章主要介紹了Python?中?Selenium?的?send_keys()?函數(shù),需要的朋友可以參考下
    2023-11-11
  • python定向爬蟲校園論壇帖子信息

    python定向爬蟲校園論壇帖子信息

    這篇文章主要為大家詳細(xì)介紹了Python定向爬蟲校園論壇帖子信息的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python常用隨機(jī)數(shù)與隨機(jī)字符串方法實(shí)例

    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 使用三種不同的簡便項窗口部件顯示數(shù)據(jù)的方法

    python3+PyQt5 使用三種不同的簡便項窗口部件顯示數(shù)據(jù)的方法

    今天小編就為大家分享一篇python3+PyQt5 使用三種不同的簡便項窗口部件顯示數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python實(shí)時監(jiān)控網(wǎng)站瀏覽記錄實(shí)現(xiàn)過程詳解

    Python實(shí)時監(jiān)控網(wǎng)站瀏覽記錄實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Python實(shí)時監(jiān)控網(wǎng)站瀏覽記錄實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • python狀態(tài)機(jī)transitions庫詳解

    python狀態(tài)機(jī)transitions庫詳解

    在用python做一個比較復(fù)雜的小項目,需要根據(jù)不同的輸入,控制攝像頭采集執(zhí)行不同的任務(wù).雖然用流程方式實(shí)現(xiàn)了,但閱讀起來費(fèi)勁,還容易出錯.所以就用了狀態(tài)機(jī),需要的朋友可以參考下
    2021-06-06
  • python 實(shí)現(xiàn)多線程下載視頻的代碼

    python 實(shí)現(xiàn)多線程下載視頻的代碼

    這篇文章主要介紹了python 實(shí)現(xiàn)多線程下載視頻的代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • python3.8與pyinstaller沖突問題的快速解決方法

    python3.8與pyinstaller沖突問題的快速解決方法

    這篇文章主要介紹了python3.8與pyinstaller沖突問題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Python學(xué)習(xí)小技巧之列表項的排序

    Python學(xué)習(xí)小技巧之列表項的排序

    這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項排序的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們可以參借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-05-05
  • Python自動安裝第三方庫的小技巧(pip使用詳解)

    Python自動安裝第三方庫的小技巧(pip使用詳解)

    很多朋友私信小編Python安裝第三方庫安裝技巧,在這就不一一回復(fù)大家了,今天小編給大家分享一篇教程關(guān)于Python自動安裝第三方庫的小技巧,本文以安裝plotly為例給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2021-05-05

最新評論

铅山县| 新疆| 宝兴县| 九龙城区| 大英县| 左云县| 昆山市| 枣庄市| 台山市| 定陶县| 崇明县| 阳城县| 九龙坡区| 商河县| 江北区| 宣城市| 饶阳县| 合山市| 多伦县| 屏东市| 甘洛县| 阿图什市| 仁怀市| 永川市| 营山县| 丹棱县| 苗栗县| 肥西县| 西平县| 玛曲县| 运城市| 凤山市| 简阳市| 滨州市| 江门市| 泾阳县| 盐亭县| 进贤县| 滁州市| 尉氏县| 沈丘县|