Python Attribute Access Order

@(Python)

data descriptor case

data descriptor is defined as having both _get_ and _set_ method

class descriptor(object):
    def __get__(self, obj, type=None):
        print('descriptor-get')
    def __set__(self, obj, value):
        print('descriptor-set')
class A(object):
    x = descriptor()
    def __getattr__(self, name):
        print('A-getattr')
    
a = A()

a.__dict__ # empty instance dictionary
-> {}

A.__dict__ # type dictionary has attribute 'x'
-> dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>,
            '__doc__': None,
            '__getattr__': <function __main__.__getattr__>,
            '__module__': '__main__',
            '__weakref__': <attribute '__weakref__' of 'A' objects>,
            'x': <__main__.descriptor at 0x1065fec10>})

a.x # data descriptor takes precedence
-> descriptor-get

A.x # visit descriptor from type or class
-> descriptor-get

del A.__dict__['x'] # disallow to del attributes in type dictionary
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-bc9fd0bf9cec> in <module>()
----> 1 del A.__dict__['x']

TypeError: 'dictproxy' object does not support item deletion

non-data descriptor case

non-data descriptor is defined as only having _get_ method

class descriptor(object):
    def __get__(self, obj, type=None):
        print('descriptor-get')
class A(object):
    x = descriptor()
    def __getattr__(self, name):
        print('A-getattr')
    
a = A()

a.x # non-data descriptor is used because there is no such attribute in instance dictionary
-> descriptor-get

A.x
-> descriptor-get

A.__dict__
-> dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>,
            '__doc__': None,
            '__getattr__': <function __main__.__getattr__>,
            '__module__': '__main__',
            '__weakref__': <attribute '__weakref__' of 'A' objects>,
            'x': <__main__.descriptor at 0x1066008d0>})

a.__dict__['x'] = 2 # set 'x' attribute into instance dictionary or a.x=2

a.x # instance dictionary takes precedence
-> 2

A.x
-> descriptor-get

_getattr_ as backup

class A(object):
    def __getattr__(self, name):
        print('A-getattr')
    
a = A()

a.x # __getattr__ is called when there is no 'x' in data descriptor, instance dictionary and non-data descriptor.
-> A-getattr
 
A.__dict__['x'] = 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-66-72c8240053eb> in <module>()
----> 1 A.__dict__['x'] = 1

TypeError: 'dictproxy' object does not support item assignment


a.__dict__['x'] = 1 # or 

a.x # instance dictionary takes precedence to __getattr__
-> 1

del a.x

a.x
-> A-getattr

A.x = descriptor() # assign a non-data descriptor into type dictionary 

A.x
-> descriptor-get

a.x # instance dictionary takes precedence to such non-data descriptor
-> 1

del a.x

a.x # non-data descriptor takes precedence to __getattr__
-> descriptor-get
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市焰雕,隨后出現(xiàn)的幾起案子蛀序,更是在濱河造成了極大的恐慌株婴,老刑警劉巖邮屁,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彩郊,死亡現(xiàn)場離奇詭異矫废,居然都是意外死亡岂膳,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門芥丧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來紧阔,“玉大人,你說我怎么就攤上這事续担∩玫ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵物遇,是天一觀的道長乖仇。 經(jīng)常有香客問我,道長询兴,這世上最難降的妖魔是什么乃沙? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮诗舰,結(jié)果婚禮上警儒,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好蜀铲,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布边琉。 她就那樣靜靜地躺著,像睡著了一般记劝。 火紅的嫁衣襯著肌膚如雪变姨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天厌丑,我揣著相機與錄音定欧,去河邊找鬼。 笑死怒竿,一個胖子當(dāng)著我的面吹牛砍鸠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播耕驰,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼睦番,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了耍属?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤巩检,失蹤者是張志新(化名)和其女友劉穎厚骗,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體兢哭,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡领舰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了迟螺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冲秽。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖矩父,靈堂內(nèi)的尸體忽然破棺而出锉桑,到底是詐尸還是另有隱情,我是刑警寧澤窍株,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布民轴,位于F島的核電站,受9級特大地震影響球订,放射性物質(zhì)發(fā)生泄漏后裸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一冒滩、第九天 我趴在偏房一處隱蔽的房頂上張望微驶。 院中可真熱鬧,春花似錦开睡、人聲如沸因苹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽容燕。三九已至梁呈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蘸秘,已是汗流浹背官卡。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留醋虏,地道東北人寻咒。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像颈嚼,于是被迫代替她去往敵國和親毛秘。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容