Within a class, a number of reserved method names have special meaning.
The most important special method name is the class
constructor, .__init__().
For a general introduction to class constructors, see Section 19, “Classes: Defining your own types”.
For old-style class constructors, see Section 19.1.2, “Instantiation of an old-style class: The
constructor, .__init__()”.
For new-style class constructors, see Section 19.2, “Life cycle of a new-style class”.
Many special methods fall into broad categories:
Section 19.3.1, “Rich comparison methods”: for methods
that implement comparisons such as “<=” and “==”.
Section 19.3.2, “Special methods for binary operators”: for
operators that operate on two operands, such as
“%”.
Section 19.3.3, “Unary operator special methods”: for
operators that operate on a single operand, such as
negation, “-”.
Section 19.3.4, “Special methods to emulate built-in functions”: for
classes that handle calls to built-in functions such
as “str()”.
The remaining special methods will be discussed in alphabetical order.
Section 19.3.5, “__call__(): What to do when someone
calls an instance”.
Section 19.3.10, “__delitem__(): Delete one item of a
sequence”.
Section 19.3.11, “__getattr__(): Handle a reference
to an unknown attribute”.
Section 19.3.12, “__getattribute__(): Intercept all
attribute references”.
Section 19.3.13, “__getitem__(): Get one item from a
sequence or mapping”.
Section 19.3.17, “__setattr__(): Intercept all
attribute changes”.
Section 19.3.18, “__setitem__(): Assign a value to
one item of a sequence”.