On 3/22/2010 11:44 AM, Bruno Desthuilliers wrote:
<snip>
Another (better IMHO) solution is to use a plain property, and store the
computed value as an implementation attribute :
@property
def foo(self):
cached = self.__dict__.get('_foo_cache')
if cached is None:
self._foo_cache = cached = self._some_time_consuming_operation()
return cached
There's no need to access __dict__ directly. I believe this is
equivalent (and clearer):
@property
def foo(self):
try:
cached = self._foo_cache
except AttributeError:
self._foo_cache = cached = self._time_consuming_op()
return cached
-John
--
http://mail.python.org/mailman/listinfo/python-list