One of the essential concepts for mastering object-oriented Python is to understand how object methods are implemented. Let's look at a relatively simple Python interaction:
>>> f = [1, 1, 2, 3]
>>> f += [f[-1] + f[-2]]
>>> f
[1, 1, 2, 3, 5]
We've created a list, f, with a sequence of values. We then mutated this list using the += operator to append a new value. The f[-1] + f[-2] expression computes the new value to be appended.
The value of f[-1] is implemented using the list object's __getitem__() method. This is a core pattern of Python: the simple operator-like syntax is implemented by special methods. The special methods have names surrounded with __ to make them distinctive. For simple prefix and suffix syntax, the object is obvious; f[-1] is implemented as f.__getitem__(-1).
The additional operation is similarly implemented by the __add__() special method. In the case of a binary operator, Python will try both operands to see which one offers the special method. In this example, both operands are integers, and both will provide a suitable implementation. In the case of mixed types, the implementation of the binary operator may coerce one value into another type. f[-1] + f[-2], then, is implemented as f.__getitem__(-1).__add__(f.__getitem__(-2)).
The update of f by the += operator is implemented by the __iadd__() special method. Consequently, f += [x] is implemented as f.__iadd__([x]).
Throughout the first eight chapters, we'll look very closely at these special methods and how we can design our classes to integrate very tightly with Python's built-in language features. Mastering the special methods is the essence of mastering object-oriented Python.