There are a total of 13 binary operators and their associated special methods. We'll focus on the obvious arithmetic operators first. The special method names match the operators (and functions), as shown in the following table:
Method |
Operator |
object.__add__(self, other) |
+ |
object.__sub__(self, other) |
- |
object.__mul__(self, other) |
* |
object.__truediv__(self, other) |
/ |
object.__floordiv__(self, other) |
// |
object.__mod__(self, other) |
% |
object.__divmod__(self, other) |
divmod() |
object.__pow__(self, other[, modulo]) |
pow() as well as ** |
And yes, interestingly, two functions are included with the various symbolic operators. There are a number of unary operators and functions, which have special method names as shown in the following table:
Method |
Operator |
object.__neg__... |