August 2018
Intermediate to advanced
332 pages
9h 12m
English
The functionality we just discussed works thanks to a magic method called __getitem__. This is the method that is called, when something like myobject[key] is called, passing the key (value inside the square brackets) as a parameter. A sequence, in particular, is an object that implements both __getitem__ and __len__, and for this reason, it can be iterated over. Lists, tuples, and strings are examples of sequence objects in the standard library.
In this section, we care more about getting particular elements from an object by a key than building sequences or iterable objects, which is a topic explored in Chapter 7, Using Generators.
If you are going to implement __getitem__ in a custom class in your domain, you ...