Jun-29-2020, 09:02 PM
Do you mean to use
__getattribute__
class YourObject: def __getattribute__(self, name): print(f'attribute {name}') return object.__getattribute__(self, name) your_instance = YourObject() your_instance.this = 'This' print(your_instance.this)
Output:attribute 'this'
This
Note: __getattr__
is called when there is no attributehttps://docs.python.org/3/reference/data..._getattr__ Wrote:object.__getattr__(self, name)
Called when the default attribute access fails with an AttributeError (either __getattribute__() raises an AttributeError because name is not an instance attribute or an attribute in the class tree for self; or __get__() of a name property raises AttributeError). This method should either return the (computed) attribute value or raise an AttributeError exception.
Note that if the attribute is found through the normal mechanism, __getattr__() is not called. (This is an intentional asymmetry between __getattr__() and __setattr__().) This is done both for efficiency reasons and because otherwise __getattr__() would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the __getattribute__() method below for a way to actually get total control over attribute access.