The following methods can be used to change the meaning of attribute access for class instances.
self
). name
is the attribute name.
Note that if the attribute is found through the normal mechanism,
__getattr__
is not called. (This is an 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, __getattr__
can fake
total control by simply not inserting any values in the instance
attribute dictionary.
name
is the attribute name, value
is the
value to be assigned to it.
If __setattr__
wants to assign to an instance attribute, it
should not simply execute self.name = value
--- this would
cause a recursive call. Instead, it should insert the value in the
dictionary of instance attributes, e.g. self.__dict__[name] =
value.
__dict__
__setattr__
but for attribute deletion instead of
assignment.