I have a class something like :
def compute():
# CPU savy task
return some_value
def compute_other():
# CPU savy task
return some_value
class myclass:
@property
def value:
# return if already computed => What can be done ?
computed_value = compute()
return computed_value
@property
def other:
# return if already computed => What can be done ?
computed_value = compute_other()
return computed_value
ob = myclass()
print ob.value, ob.other
print ob.value, ob.other
print ob.value, ob.other
How can I intercept the method call to value and other and return the values if already available.
Can __getattribute__(self,name) be used to intercept any property and lookup if already available in self.__dict__ ?
Implement a memoization decorator and just apply it to your class:
http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize