Inconsistent behavior of descriptors

Denis S. Otkidach ods at strana.ru
Tue Sep 30 18:15:10 CEST 2003


I've noticed that the order of attribute lookup is inconsistent
when descriptor is used.  property instance takes precedence of
instance attributes:

>>> class A(object):
...     def _get_attr(self):
...         return self._attr
...     attr = property(_get_attr)
...
>>> a=A()
>>> a.__dict__
{}
>>> a.__dict__['attr']=1
>>> a.__dict__
{'attr': 1}
>>> a.attr
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in _get_attr
AttributeError: 'A' object has no attribute '_attr'

But it doesn't when I use custom class of descriptor:

>>> class descr(object):
...     def __get__(self, inst, cls):
...         return inst._attr
...
>>> class B(object):
...     attr = descr()
...
>>> b=B()
>>> b.__dict__
{}
>>> b.__dict__['attr']=1
>>> b.__dict__
{'attr': 1}
>>> b.attr
1

Subclasses of property behave like property itself:

>>> class descr2(property):
...     def __get__(self, inst, cls):
...         return inst._attr
...
>>> class C(object):
...     attr = descr2()
...
>>> c=C()
>>> c.__dict__
{}
>>> c.__dict__['attr']=1
>>> c.__dict__
{'attr': 1}
>>> c.attr
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in __get__
AttributeError: 'C' object has no attribute '_attr'

Is it an undocumented feature or I have to submit a bug report?

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]






More information about the Python-list mailing list