Essentially I want to do something like this:
class foo:
x = 4
@property
@classmethod
def number(cls):
return x
Then I would like the following to work:
>>> foo.number
4
Unfortunately, the above doesn’t work. Instead of given me 4 it gives me <property object at 0x101786c58>. Is there any way to achieve the above?
The
propertydescriptor always returns itself when accessed from a class (ie. wheninstanceisNonein its__get__method).If that’s not what you want, you can write a new descriptor that always uses the class object (
owner) instead of the instance: