[Python-Dev] assymetry in descriptor behavior

Guido van Rossum guido@python.org
Sun, 23 Feb 2003 18:19:51 -0500


> I notice that Python supports this sort of dual access for reading
> attributes and calling static functions, but getting that behavior for
> mutable attributes seems unreasonably difficult: I need a property in
> the metaclass *and* in the class.

I disagree that it is *unreasonably* difficult.  Given that Python
tries to do with a single notation ('.') where C++ has two notations
('.' and '::') to disambiguate cases, not to mention declarations, I
think it is reasonable that this unusual case requires a little more
effort; you should be glad that it's possible at all. :-)

> 1. To throw out a straw-man suggestion, what about adding an
>    additional protocol __set2__ which, if found, will be called
>    instead of __set__ both for reading _and_ writing attributes on the
>    class?

Let me throw out this straw-man right away: I'm not excited about
this.  You can write a metaclass that implements this generically
though.

> 2. What are the optional type=None arguments for?  It seems as though
>    only the middle argument (obj) is ever None.  I just copied this
>    protocol out of descrintro.html

Only __get__ has both obj and type as arguments; __set__ has obj and
value, __delete__ only obj.

__get__ has obj and type because it can be used for instance and class
attribute access.  When called for a class, obj is None because it is
unavailable; but when called for an instance, type is set to obj's
class, for the convenience of descriptors that aren't interested in
the instance (like staticmethod and classmethod).

> 3. Is there documentation for __delete__ anywhere?

Apparently not, but it's easy to guess what it does if you know
__getattr__, __setattr__ and __delattr__.  It's __delete__ and not
__del__ because __del__ is already taken.  In an early alpha release
it was actually __del__, but that didn't work very well. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)