亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

_getattr_vs_getAttribute_

_getattr_vs_getAttribute_

Helenr 2019-11-07 10:06:29
_getattr_vs_getAttribute_我想弄清楚什么時候該用__getattr__或__getattribute__..這個文獻資料提及__getattribute__適用于新樣式的類。什么是新類型的課程?
查看完整描述

3 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

兩個主要的區別__getattr____getattribute__那是__getattr__只有在沒有按照通常的方式找到屬性時才調用。它有利于實現丟失屬性的回退,而且可能是您想要的兩個屬性中的一個。

__getattribute__在查看對象上的實際屬性之前調用,因此要正確實現可能很困難。您可以很容易地在無限遞歸中結束。

新樣式類派生自object,舊式類是Python2.x中沒有顯式基類的類。但是,在選擇新舊類時,區分舊類和新類并不是重要的區別。__getattr____getattribute__.

你幾乎肯定想__getattr__.



查看完整回答
反對 回復 2019-11-08
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

讓我們看看這兩個方面的一些簡單示例。__getattr____getattribute__神奇的方法。

__getattr__

Python會調用__getattr__方法,只要請求尚未定義的屬性。在下面的示例中,我的類數數__getattr__方法?,F在,主要是當我試圖訪問兩個obj1.myminobj1.mymax屬性一切都很好。但當我試圖進入obj1.mycurrent屬性-Python給了我AttributeError: 'Count' object has no attribute 'mycurrent'

class Count():
    def __init__(self,mymin,mymax):
        self.mymin=mymin
        self.mymax=mymax

obj1 = Count(1,10)print(obj1.mymin)print(obj1.mymax)print(obj1.mycurrent)  --> AttributeError: 'Count' object has no attribute 'mycurrent'

現在我的課數數__getattr__方法?,F在當我試圖訪問obj1.mycurrent屬性-python返回我在__getattr__方法。在我的示例中,每當我試圖調用不存在的屬性時,python將創建該屬性并將其設置為整數值0。

class Count:
    def __init__(self,mymin,mymax):
        self.mymin=mymin
        self.mymax=mymax    

    def __getattr__(self, item):
        self.__dict__[item]=0
        return 0obj1 = Count(1,10)print(obj1.mymin)print(obj1.mymax)print(obj1.mycurrent1)

__getattribute__

現在讓我們看看__getattribute__方法。如果你有__getattribute__方法在類中,python對每個屬性調用此方法,不管它是否存在。所以為什么我們需要__getattribute__方法?一個很好的原因是,您可以阻止對屬性的訪問,并使它們更加安全,如下面的示例所示。

每當有人試圖訪問我的屬性時,都以子字符串開頭。‘cur’Python引發AttributeError例外。否則,它將返回該屬性。

class Count:

    def __init__(self,mymin,mymax):
        self.mymin=mymin
        self.mymax=mymax
        self.current=None

    def __getattribute__(self, item):
        if item.startswith('cur'):
            raise AttributeError
        return object.__getattribute__(self,item) 
        # or you can use ---return super().__getattribute__(item)obj1 = Count(1,10)print(obj1.mymin)print(obj1.mymax)print(obj1.current)

重要:為了避免無限遞歸__getattribute__方法時,它的實現應該始終調用同名的基類方法來訪問它需要的任何屬性。例如:object.__getattribute__(self, name)super().__getattribute__(item)而不是self.__dict__[item]

重要

如果您的類同時包含兩個蓋塔getAttribute魔法方法__getattribute__被稱為第一個。但如果__getattribute__提高AttributeError異常,則該異常將被忽略,并且__getattr__方法將被調用。請參見以下示例:

class Count(object):

    def __init__(self,mymin,mymax):
        self.mymin=mymin
        self.mymax=mymax
        self.current=None

    def __getattr__(self, item):
            self.__dict__[item]=0
            return 0

    def __getattribute__(self, item):
        if item.startswith('cur'):
            raise AttributeError
        return object.__getattribute__(self,item)
        # or you can use ---return super().__getattribute__(item)
        # note this class subclass objectobj1 = Count(1,10)print(obj1.mymin)print(obj1.mymax)print(obj1.current)



查看完整回答
反對 回復 2019-11-08
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

這只是一個基于奈德·巴奇爾德的解釋.

__getattr__例子:

class Foo(object):
    def __getattr__(self, attr):
        print "looking up", attr
        value = 42
        self.__dict__[attr] = value        return value

f = Foo()print f.x 
#output >>> looking up x 42f.x = 3print f.x 
#output >>> 3print ('__getattr__ sets a default value if undefeined OR __getattr__ to define how to handle attributes that are not found')

如果使用相同的示例__getattribute__你會得到>RuntimeError: maximum recursion depth exceeded while calling a Python object



查看完整回答
反對 回復 2019-11-08
  • 3 回答
  • 0 關注
  • 499 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號