class myclass(object): def __init__(self): self.__age=None @property def age(self): if self.__age is None: self.__age=21
Alex mentioned that you can use __getattr__ , this is how it works
class myclass(object): def __getattr__(self, attr): if attr=="age": self.age=21
__getattr__() is called when the attribute does not exist on the object, i.e. at the first attempt to access age . Each time after this age exists, therefore __getattr__ does not receive a call
John la rooy
source share