We'll now look at the three canonical special methods for attribute access: __getattr__(), __setattr__(), and __delattr__(). Additionally, we'll acknowledge the __dir__() special method to reveal attribute names. We'll defer __getattribute__() to the next section.
The default behavior shown in this section is as follows:
- The __setattr__() method will create and set attributes.
- The __getattr__() method is a fallback used when an attribute is not defined. When an attribute name is not part of the instance variables of an object, then the __getattr__() method is used. The default behavior of this special method is to raise an AttributeError exception. We can override this to return a meaningful result instead of raising an exception.
- The __delattr__() method deletes an attribute.
- The __dir__() method returns a list of attribute...