10.4. Reflection Getattr
Get Attribute If Missing
Called whenever you request an attribute that hasn't already been defined
It will not execute, when attribute already exist
Implementing a fallback for missing attributes
10.4.1. Example
>>> class Point:
... x: int
... y: int
... z: int
...
... def __getattr__(self, attrname):
... print(f'Attribute {attrname} does not exist')
... super().__setattr__(attrname, 0)
... print(f'Attribute {attrname} initialized with value 0')
>>> pt = Point()
>>> vars(pt)
{}
>>> pt.x
Attribute x does not exist
Attribute x initialized with value 0
>>>
>>> pt.x
0
>>> vars(pt)
{'x': 0}