[Python-ideas] Properties for classes possible?

Erik Bray erik.m.bray at gmail.com
Thu Aug 20 15:51:35 CEST 2015


On Thu, Aug 20, 2015 at 2:54 AM, Thomas Güttler
<guettliml at thomas-guettler.de> wrote:
> I think it would be great to have properties for classes in Python2 and
> Python3
>
> There are some "patterns" to get this working:
>
>
> http://stackoverflow.com/questions/5189699/how-can-i-make-a-class-property-in-python
>
>   http://stackoverflow.com/questions/128573/using-property-on-classmethods
>
> ... but an official solution would be more "zen of python".

I recently added my own implementation of this here:

https://github.com/astropy/astropy/blob/master/astropy/utils/decorators.py#L295

(which I think is pretty elegant--moreso than the ones on those SO
links, but I'm biased :)

However, my version is only for read-only properties.  It could easily
be extended to support writeable properties, but as pointed out
elsewhere that requires the container class to have a custom metaclass
:/

> Do you think properties for classes would be useful?

Yes--given my recent experience in trying to implement this I feel
like it should be supported in Python.  Just as Python ships with a
builtin classmethod, I think there should be a builtin classproperty
that is special-cased to work without a custom metaclass.  I think
it's a special case that's special enough IMO.


> If it works for classes, then it could be used for modules, too?

With a ModuleType subclass, sure, but that's a separate, and thornier issue :)

Erik


More information about the Python-ideas mailing list