Sometimes I like to write getter attributes for an object such that the first time they are called, the heavy lifting is done once, and that value is saved and returned on future calls. In objective-c I would use an ivar or a static variable to hold this value. Something like:
- (id)foo
{
if ( _foo == nil )
{
_foo = // hard work to figure out foo
}
return _foo
}
Does this same pattern hold up well in Python, or is there a more accepted way of doing this? I have basically the same thing so far. What I don't like about my solution is that my object gets cluttered up with values and getters for those values:
def foo(self):
if not hasattr(self, "mFoo":
self.mFoo = # heavy lifting to compute foo
return self.mFoo