Operator Overloading Methods
Classes intercept and implement built-in operations by providing specially named method functions, which all start and end with two underscores. These names are not reserved and may be inherited from superclasses as usual. At most one is located and called per operation.
Python automatically calls a class’s overloading methods when
instances appear in expressions and other contexts. For example, if a
class defines a method named
_ _getitem_ _, and
X is an instance of this class, then the
X[i] is equivalent to the method call
X._ _getitem_ _(i).
Overloading method names are sometimes arbitrary: a class’s
_ _add_ _ method need not perform an addition (or
concatenation). Moreover, classes generally may mix numeric and
collection methods and mutable and nonmutable operations.
For All Types
_ _init_ _(self [, arg]*)
class(args...). Constructor: initializes the new instance,
_ _del_ _(self)
On instance garbage collection. Cleans up when instance is freed. Embedded objects are automatically freed when parent is (unless referenced from elsewhere).
_ _repr_ _(self)
_ _str_ _). Returns string representation.
_ _str_ _(self)
_ _repr_ _if defined). Returns string representation.
_ _cmp_ _(self, other), _ _rcmp_ _
x), etc. Called for all comparisons for which no more specific method (such as
_ _lt_ _) is defined or inherited (see rich comparison ...