python 描述符_python数据和非数据描述符

Data descriptors with __set__() and __get__() defined always override a redefinition in an instance dictionary.

I have no problem understanding this sentence, but can someone clarify for me why such a rule is in place? After all, if I want to override an attribute in an instance dictionary, I already need to do that explicitely (inst.__dict__["attr"] = val), as a naive inst.attr = val would call the descriptor's __set__ method, which would (usually) not override the attribute in the instance dictionary.

edit: just to make it clear, I understand what is happening, my question is about why such a rule was put in place.

解决方案

The override applies to descriptors that are part of the class __dict__.

Python will always look up type(instance).__dict__[attributename].__get__(instance, type(instance)), and will not use instance.__dict__ to search for a instance-override.

Here is an example using a contrived Descriptor class and a property (which is a descriptor with a __get__ and a __set__:

>>> class Descriptor(object):

... def __init__(self, name):

... self.name = name

... def __get__(self, instance, cls):

... print 'Getting %s, with instance %r, class %r' % (self.name, instance, cls)

...

>>> class Foo(object):

... _spam = 'eggs'

... @property

... def spam(self):

... return self._spam

... @spam.setter

... def spam(self, val):

... self._spam = val

...

>>> Foo().spam

'eggs'

>>> foo = Foo()

>>> foo.__dict__['spam'] = Descriptor('Override')

>>> foo.spam

'eggs'

As you can see, even though I add a spam entry in the instance __dict__, it is completely ignored and the Foo.spam property is used still. Python is ignoring the instance __dict__ because the spam property defines both __get__ and a __set__.

If you use a descriptor that doesn't define a __set__ the override works (but it's __get__ is not called:

>>> class Foo(object):

... desc = Descriptor('Class-stored descriptor')

...

>>> Foo.desc

Getting Class-stored descriptor, with instance None, class

>>> Foo().desc

Getting Class-stored descriptor, with instance <__main__.foo object at>, class

>>> foo = Foo()

>>> foo.__dict__['desc'] = Descriptor('Instance-stored descriptor')

>>> foo.desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值