Python - Class Operator Overloading Methods


The following table lists operator methods you can overload in your own class.

Method                 Implements                         Called for 

__init__               Constructor                        Object creation: X = Class(args) 

__del__                Destructor                         Object reclamation of X 

__add__                Operator +                         X + Y, X += Y if no __iadd__ 

__or__                 Operator  | (bitwise OR)           X | Y, X |= Y if no __ior__ 

__repr__, __str__      Printing, conversions              print(X), repr(X), str(X) 

__call__               Function calls                     X(*args, **kargs) 

__getattr__            Attribute fetch                    X.undefined 

__setattr__            Attribute assignment               X.any = value 

__delattr__            Attribute deletion                 del X.any 

__getattribute__       Attribute fetch                    X.any 

__getitem__            Indexing, slicing, iteration       X[key], X[i:j], for loops and other iterations if no __iter__ 

__setitem__            Index and slice assignment         X[key] = value, X[i:j] = iterable 

__delitem__            Index and slice deletion           del X[key], del X[i:j] 

__len__                Length                             len(X), truth tests if no __bool__ 

__bool__               Boolean tests                      bool(X), truth tests (named __nonzero__ in 2.X) 

__lt__, __gt__,        Comparisons                        X < Y, X > Y, X <= Y, X >= Y, X == Y, X != Y 
__le__, __ge__,                                           (or else __cmp__ in 2.X only) 
__eq__, __ne__ 

__radd__               Right-side operators               Other + X 

__iadd__               In-place augmented operators       X += Y (or else __add__) 

__iter__, __next__     Iteration contexts                 I=iter(X), next(I); for loops,  in if no __con 
                                                          tains__, all comprehensions, map(F,X), others 
                                                          (__next__ is named  next in 2.X) 

__contains__           Membership test                    item in X (any iterable) 

__index__              Integer value                      hex(X), bin(X), oct(X), O[X], O[X:] (replaces 2.X 
                                                          __oct__, __hex__) 

__enter__, __exit__    Context manager (Chapter 34)       with obj as var: 

__get__, __set__,      Descriptor attributes (Chapter 38) X.attr, X.attr = value, del X.attr 

__new__                Creation (Chapter 40)              Object creation, before __init__ 

All overloading methods have names that start and end with two underscores to keep them distinct from other names you define.

Operator overloading methods may be inherited from superclasses.

Operator overloading methods are all optional.

If you don't code or inherit one, that operation is simply unsupported by your class, and attempting it will raise an exception.