[Python-ideas] @classproperty, @abc.abstractclasspropery, etc.

Nick Coghlan ncoghlan at gmail.com
Tue Jan 4 15:16:55 CET 2011


On Tue, Jan 4, 2011 at 7:09 AM, K. Richard Pixley <rich at noir.com> wrote:

>  I think the meanings of the new ones are pretty straightforward, but in
> case they are not...
>
> @staticproperty - like @property only without an implicit first argument.
> Allows the property to be called directly from the class without requiring a
> throw-away instance.
>
> @classproperty - like @property, only the implicit first argument to the
> method is the class.  Allows the property to be called directly from the
> class without requiring a throw-away instance.
>

As Michael mentions later in the thread, these can't really work due to the
asymmetry in the descriptor protocol: if you retrieve a descriptor object
directly from a class, the interpreter will consult the __get__ method of
that descriptor, but if you set or delete it through the class, it will just
perform the set or delete - the descriptor has no say in the matter, even if
it defines __set__ or __delete__ methods. (See the example interpreter
session at http://pastebin.com/1M7KYB9d).

The only way to get static or class properties to work correctly is to
define them on the metaclass, in which case you can just use the existing
property descriptor (although you have to then jump through additional hoops
to make access via instances work properly - off the top of my head, I'm
actually not sure how to make that happen).

@abc.abstractattribute - a simple, non-callable variable that must be
> overridden in subclasses
>

You can't decorate attributes, only functions.


> @abc.abstractstaticproperty - like @abc.abstractproperty only for
> @staticproperty
>
> @abc.abstractclassproperty - like @abc.abstractproperty only for
> @classproperty
>

See above. These don't exist because staticproperty and classproperty don't
work.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110105/d7c62695/attachment.html>


More information about the Python-ideas mailing list