Inconsistent behavior of descriptors
John Roth
newsgroups at jhrothjr.com
Wed Oct 1 07:11:16 EDT 2003
"Denis S. Otkidach" <ods at strana.ru> wrote in message
news:mailman.1065003124.23596.python-list at python.org...
> On Tue, 30 Sep 2003, John Roth wrote:
>
> JR> > I've noticed that the order of attribute lookup is
> JR> inconsistent
> JR> > when descriptor is used. property instance takes
> JR> precedence of
> JR> > instance attributes:
> [...]
> JR> > But it doesn't when I use custom class of descriptor:
> [...]
> JR> I'm not entirely sure what you're describing, however, I
> JR> notice that your descriptor object has a __get__ method,
> JR> but no __set__ or __del__ methods. The lookup chain
> JR> is quite different for descriptors with only __get__ methods
> JR> than it is for descriptors for all three.
> JR>
> JR> In particular, it's intended that instance attributes should
> JR> be ignored if there is a data descriptor (that is, a
> JR> descriptor
> JR> with both__get__ and __set__ methods.)
>
> Is it documented somewhere? What is the reason for such
> inconsistent behavior?
The behavior of descriptors is documented in the article on
the python web site. You might have to hunt a bit to find it.
The reason for the inconsistency is simply that descriptors
with only a __get__ method are used for methods, functions
and similar entities, while descriptors with both __get__ and
__put__ are used for properties and similar entities.
It's useful to be able to put a unique version of a function
or method in an instance, it would completely subvert the
meaning of a property if you could do so.
John Roth
>
> --
> Denis S. Otkidach
> http://www.python.ru/ [ru]
>
>
More information about the Python-list
mailing list