I like the cached_property
module: https://pypi.python.org/pypi/cached-property
Is there a way to get cached properties for classes?
Here is an example for "properties for classes"
class Foo(object):
@classmethod
def get_bar(cls):
return 'bar'
@cached_class_property
def bar(cls):
return 'bar'
Usage:
assert Foo.bar == Foo.get_bar()
With "cached" I mean that the second (and later) calls to the property returns the value of the first call.
Properties are nothing but descriptors, and when we define a descriptor it is always looked up on the type of the object. For instances a descriptor will be looked up on its class and similarly for a class it is going to be looked up on its type, i.e Metaclass.
Demo: