Next / Previous / Contents / TCC Help System / NM Tech homepage

26.3.2. Special methods for binary operators

Your class can define special methods with these names to tell Python how to handle binary operators such as “*” or “%”. In each case, the calling sequence will look something like this:

    def __method__(self, other):
        ...

The self argument is the left-hand operand, and the other argument is the right-hand operand. Your method will return the result of the operation.

For each operator, you may supply up to three methods:

OperatorNormalReversedAugmented
+__add____radd____iadd__
&__and____rand____iand__
/__div____rdiv____idiv__
//__floordiv____rfloordiv____ifloordiv__
<<__lshift____rlshift____ilshift__
%__mod____rmod____imod__
*__mul____rmul____imul__
|__or____ror____ior__
**__pow____rpow____ipow__
>>__rshift____rrshift____irshift__
-__sub____rsub____isub__
^__xor____rxor____ixor__