December 2018
Beginner to intermediate
796 pages
19h 54m
English
I find Python's approach to operator overloading to be brilliant. To overload an operator means to give it a meaning according to the context in which it is used. For example, the + operator means addition when we deal with numbers, but concatenation when we deal with sequences.
In Python, when you use operators, you're most likely calling the special methods of some objects behind the scenes. For example, the a[k] call roughly translates to type(a).__getitem__(a, k).
As an example, let's create a class that stores a string and evaluates to True if '42' is part of that string, and False otherwise. Also, let's give the class a length property that corresponds to that of the stored string:
# oop/operator.overloading.py ...Read now
Unlock full access