成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

python 屬性搜索優(yōu)先級(jí)和descriptor

NusterCache / 920人閱讀

摘要:屬性搜索優(yōu)先級(jí)和定義數(shù)據(jù)描述符數(shù)據(jù)描述符非數(shù)據(jù)描述符一般情況數(shù)據(jù)型描述符非數(shù)據(jù)型描述符類調(diào)用描述符如果被重寫上面代碼介紹的東西會(huì)被覆蓋掉除非函數(shù)內(nèi)部調(diào)用否則別這么干例子

屬性搜索優(yōu)先級(jí)和descriptor
########################
# 定義
########################

# 數(shù)據(jù)描述符
class DataDesc(object):
    def __init__(self, *args, **kwargs):
        pass

    def __get__(self, instance, cls):
        print(instance is None)
        return "DataDesc"

    def __set__(self, instance, value):
        return None

    def __delete__(self, instance):
        return None


# 數(shù)據(jù)描述符
class DataDesc2(object):
    def __init__(self, *args, **kwargs):
        pass

    def __get__(self, instance, cls):
        return "DataDesc2"

    def __set__(self, instance, value):
        return None


# 非數(shù)據(jù)描述符
class NonDataDesc(object):
    def __init__(self, *args, **kwargs):
        pass

    def __get__(self, instance, cls):
        print(instance is None)
        return "NonDataDesc"


class Foo(object):
    test_attr = DataDesc()
    test_attr2 = DataDesc()
    test_attr3 = NonDataDesc()
    test_attr4 = "test_attr4"

    def __getattr__(self, item):
        return "hehe__getattr__"


########################
# 一般情況
########################
foo = Foo()
"""
1. foo.__dict__["test_attr4"]
2. for cls in Foo.__mro__:
       cls.__dict__["test_attr4"]
3. foo.__getattr__("test_attr4")
"""
print(foo.test_attr4)
foo.__dict__["test_attr4"] = 666
print(foo.test_attr4)
print(id(foo.test_attr4) == id(foo.__dict__["test_attr4"]))
foo.__dict__.pop("test_attr4")
print(foo.test_attr4)
print(foo.test_attr5)
print(foo.__getattr__("test_attr5"))

########################
# 數(shù)據(jù)型描述符
########################
foo = Foo()
"""
1. type(foo).__dict__["test_attr"].__get__(foo, type(foo))
2. foo.__dict__["test_attr4"]
3. for cls in Foo.__mro__:
       cls.__dict__["test_attr4"]
4. foo.__getattr__("test_attr4")
"""
print(foo.test_attr)
foo.__dict__["test_attr"] = 666
print(foo.test_attr)
print(id(foo.test_attr) == id(foo.__dict__["test_attr"]))
delattr(Foo, "test_attr")
print(foo.test_attr)
print(id(foo.test_attr) == id(foo.__dict__["test_attr"]))

########################
# 非數(shù)據(jù)型描述符
########################
foo = Foo()
"""
1. foo.__dict__["test_attr4"]
2. type(foo).__dict__["test_attr"].__get__(foo, type(foo))
3. for cls in Foo.__mro__:
       cls.__dict__["test_attr4"]
4. foo.__getattr__("test_attr4")
"""
print(foo.test_attr3)
foo.__dict__["test_attr3"] = 666
print(foo.test_attr3)
print(id(foo.test_attr3) == id(foo.__dict__["test_attr3"]))
del foo.test_attr3
print(foo.test_attr3)

########################
# 類調(diào)用描述符
########################
"""
FOO.__dict__["test_attr"].__get__(None, FOO)
"""
print(Foo.test_attr2)
print(Foo.test_attr3)

########################
# __getattribute__
########################
"""
如果被重寫,上面代碼介紹的東西會(huì)被覆蓋掉
除非函數(shù)內(nèi)部調(diào)用object.__getattribute__
否則別這么干
"""


class Foo2(object):
    test_attr = DataDesc()
    test_attr2 = DataDesc()
    test_attr3 = NonDataDesc()
    test_attr4 = "test_attr4"

    def __getattribute__(self, item):
        return "in __getattribute__"


foo = Foo2()
print(foo.test_attr3)

########################
# 例子
########################
import time


class LazyProperty(object):
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, cls):
        if instance is not None:
            val = self.func(instance)
            setattr(instance, self.func.__name__, val)
            return val


class Foo3(object):
    @LazyProperty
    def method(self):
        time.sleep(5)
        return 666


foo3 = Foo3()
time1 = time.time()
print(foo3.method)
time2 = time.time()
print(time2 - time1)
print(foo3.method)
print(time.time() - time2)

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/45184.html

相關(guān)文章

  • 如何正確地使用Python屬性描述符

    摘要:描述符登場什么是描述符一般來說,描述符是一個(gè)具有綁定行為的對象屬性,其屬性的訪問被描述符協(xié)議方法覆寫。先看如何用描述符來解決上面邏輯重復(fù)的問題。 關(guān)于@property裝飾器 在Python中我們使用@property裝飾器來把對函數(shù)的調(diào)用偽裝成對屬性的訪問。 那么為什么要這樣做呢?因?yàn)锧property讓我們將自定義的代碼同變量的訪問/設(shè)定聯(lián)系在了一起,同時(shí)為你的類保持一個(gè)簡單的訪問...

    huayeluoliuhen 評論0 收藏0
  • Python中類的屬性具有惰性求值的能力

    摘要:更多描述可見文檔這種惰性求值的方法在很多模塊中都會(huì)使用,比如中的使用上與例子一致,如表單中的討論在大部分情況下,讓屬性具有惰性求值能力的全部意義就在于提升程序性能。當(dāng)不需要這個(gè)屬性時(shí)就能避免進(jìn)行無意義的計(jì)算,同時(shí)又能阻止該屬性重復(fù)進(jìn)行計(jì)算。 起步 我們希望將一個(gè)只讀的屬性定義為 property 屬性方法,只有在訪問它時(shí)才進(jìn)行計(jì)算,但是,又希望把計(jì)算出的值緩存起來,不要每次訪問它時(shí)都重...

    NervosNetwork 評論0 收藏0
  • Pythondescriptor(上)

    摘要:事實(shí)上實(shí)例的實(shí)現(xiàn)方式與上面的實(shí)例類似。其次,為了實(shí)現(xiàn)確實(shí)對屬性的調(diào)用順序做出了相應(yīng)的調(diào)整,這些將會(huì)的下中介紹。參考資料如何理解的中基于的一些概念上中基于的一些概念下的官方文檔描述符解密的官方文檔 Python 在 2.2 版本中引入了descriptor(描述符)功能,也正是基于這個(gè)功能實(shí)現(xiàn)了新式類(new-styel class)的對象模型,同時(shí)解決了之前版本中經(jīng)典類 (classi...

    yexiaobai 評論0 收藏0
  • opencv python 特征匹配

    摘要:匹配器匹配非常簡單,首先在第一幅圖像中選取一個(gè)關(guān)鍵點(diǎn)然后依次與第二幅圖像的每個(gè)關(guān)鍵點(diǎn)進(jìn)行描述符距離測試,最后返回距離最近的關(guān)鍵點(diǎn)對于匹配器,首先我們必須使用創(chuàng)建對象。 Feature Matching Brute-Force匹配器 Brute-Force匹配非常簡單,首先在第一幅圖像中選取一個(gè)關(guān)鍵點(diǎn)然后依次與第二幅圖像的每個(gè)關(guān)鍵點(diǎn)進(jìn)行(描述符)距離測試,最后返回距離最近的關(guān)鍵點(diǎn). 對于...

    macg0406 評論0 收藏0
  • JS 裝飾器,一篇就夠

    摘要:的裝飾器中的同樣借鑒了這個(gè)語法糖,不過依賴于的方法。等同于也就是說,裝飾器是一個(gè)對類進(jìn)行處理的函數(shù)。別名或裝飾器在控制臺(tái)顯示一條警告,表示該方法將廢除。有了裝飾器,就可以改寫上面的代碼。 更多文章,請?jiān)贕ithub blog查看 在 ES6 中增加了對類對象的相關(guān)定義和操作(比如 class 和 extends ),這就使得我們在多個(gè)不同類之間共享或者擴(kuò)展一些方法或者行為的時(shí)候,變得并...

    learning 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<