The arithmetic operator's special methods

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 ...

Get Mastering Object-oriented Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.