This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Want to make a class method a property by combining decorators
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, ncoghlan, the.mulhern
Priority: normal Keywords:

Created on 2014-02-17 13:43 by the.mulhern, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg211416 - (view) Author: the mulhern (the.mulhern) Date: 2014-02-17 13:43
The problems is that it is quite possible to define a property using @property in a class and then later to realize that it really ought to be a class method, not an instance method. But then, if you change it to a class method, using @classmethod annotation, the @property annotation will fail to work. If you are a pedantic person, and name your properties differently than you name your methods, you have to:
1) Change all uses of this former property back to method invocations, with a "()"
2) Change the name, so it now reflects that it is an actual function, not a property any longer.

I think an @classproperty and an @staticproperty decorator would take this problem away.

An alternative would be to make it possible to chain @property and @classmethod. I know that this is sort of doable for ABC.abstractproperty which is now deprecated in favor of @property combined with @ABC.abstractmethod. This would be ideal, but I can not pretend to know if it would be possible. Also, I suspect that the order would matter as it does for ABC as described in
msg237702 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-03-09 20:43
@"the mulhern" sorry that this slipped under our radar.

@Nick I've put you on the nosy list as you actioned #16267 which is referenced in msg211416.
msg237754 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-03-10 12:07
To get the behaviour you're requesting, you need to use a custom metaclass and define the property there. The reason is that the descriptor machinery is bypassed entirely when setting or deleting an attribute on the class itself:

>>> class Example:
...     @property
...     def p(self):
...         return 1
>>> Example.p
<property object at 0x7f0901d7d548>
>>> Example().p
>>> Example().p = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>> Example.p = 2
>>> Example.p
>>> Example().p
>>> Example().p = 3

Hence, the only way to get a "class property" is to use the normal @property descriptor in a custom metaclass (i.e. the class-of-the-class)
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64858
2015-03-10 12:07:54ncoghlansetstatus: open -> closed
resolution: not a bug
messages: + msg237754

stage: resolved
2015-03-09 20:43:20BreamoreBoysetnosy: + BreamoreBoy, ncoghlan

messages: + msg237702
versions: + Python 3.5, - Python 3.1, Python 2.7, Python 3.2, Python 3.3, Python 3.4
2014-02-17 13:43:57the.mulherncreate