Computing class variable on demand?
fortepianissimo
fortepianissimo at gmail.com
Thu Feb 3 01:08:18 EST 2005
We all know that using __getattr__() we can compute an instance
variable on demand, for example:
class Foo:
def __getattr__ (self, name):
if name == 'bar':
self.bar = 'apple'
return self.bar
else:
raise AttributeError()
Then we can
f = Foo()
s1 = f.bar
s2 = f.bar # this uses the "cached" result
My question is can we do the same with class variables? I can't find a
class-equivalent __getattr__ for this purpose. The key is to allow the
use of the following statement
Foo.bar # bar is a class variable of Foo computed on demand
without unnecessary fudge. Thanks.
More information about the Python-list
mailing list