August 2018
Intermediate to advanced
332 pages
9h 12m
English
As we have just seen, if an object implements the __iter__() magic method, it means it can be used in a for loop. While this is a great feature, it's not the only possible form of iteration we can achieve. When we write a for loop, Python will try to see if the object we're using implements __iter__, and, if it does, it will use that to construct the iteration, but if it doesn't, there are fallback options.
If the object happens to be a sequence (meaning that it implements __getitem__() and __len__() magic methods), it can also be iterated. If that is the case, the interpreter will then provide values in sequence, until the IndexError exception is raised, which, analogous to the aforementioned StopIteration ...