You can memoize the result and store it as a property: class Foo(object): def __init__(self): self._big_calc_result = None @property def big_calc_result(self): if self._big_calc_result is not None: return self._big_calc_result else: self._big_calc_result = self.big_calc_run() return self._big_calc_result def big_calc_run(self): time.sleep(10) # Takes a long time ... Now, you just initialize the class and...
The fact that it works on a class in addition to an instance is intentional. In fact, hasattr should work on any object that you pass to it. Basically, it does something like this: def hasattr(obj, attribute): try: getattr(obj, attribute) except: return False return True You can rely on that...