10.5. Reflection Getattribute
Get Attribute if Exists
Called for every time, when you want to access any attribute
Before even checking if this attribute exists
If attribute is not found, then raises
AttributeErrorand calls__getattr__()Typing
obj.attrnameWill call
getattr(obj, attrname)Which triggers
obj.__getattribute__(attrname)If ok: then return value
If error: then call
obj.__getattr__(attrname)
10.5.1. Example
>>> class Point:
... x: int
... y: int
... z: int
...
... def __getattribute__(self, attrname):
... if attrname == 'y':
... raise PermissionError(f'Attribute {attrname} access is forbidden')
... return super().__getattribute__(attrname)
>>> pt = Point()
>>> pt.x = 1
>>> pt.y = 2
>>> pt.z = 3
>>> pt.x
1
>>> pt.y
Traceback (most recent call last):
PermissionError: Attribute y access is forbidden