A class can implement certain operations that are invoked by special
syntax (such as subscription or arithmetic operations) by defining
methods with special names. For instance, if a class defines a
method named __getitem__
, and x
is an instance of this
class, then x[i]
is equivalent to x.__getitem__(i)
.
(The reverse is not true --- if x
is a list object,
x.__getitem__(i)
is not equivalent to x[i]
.)
Except for __repr__
, __str__
and __cmp__
,
attempts to execute an
operation raise an exception when no appropriate method is defined.
For __repr__
, the default is to return a string describing the
object's class and address.
For __cmp__
, the default is to compare instances based on their
address.
For __str__
, the default is to use __repr__
.