June 2019
Beginner to intermediate
770 pages
19h 24m
English
The arithmetic operators (+, -, *, /, //, %, **, and so on) all map to special method names. When we provide an expression such as 355+113, the generic + operator will be mapped to a concrete __add__() method of a specific numeric class. This example will turn out to be evaluated as though we had written 355.__add__(113). The simplest rule is that the left-most operand determines the class of the operator being used.
But wait, there's more! When we have an expression with mixed types, Python may end up with two available implementations of the special method, one in each class. Consider 7-0.14 as an expression. Using the left-side int class, this expression will be attempted as 7.__sub__(0.14) ...
Read now
Unlock full access