Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Later in the book, we’ll see that user-defined classes can implement the iteration protocol too. Because of this, it’s sometimes important to know which built-in tools make use of it—any tool that employs the iteration protocol will automatically work on any built-in type or user-defined class that provides it.
So far, I’ve been demonstrating iterators in the context of the
for loop statement, because this
part of the book is focused on statements. Keep in mind, though, that
every tool that scans from left to right across objects uses the
iteration protocol. This includes the for loops we’ve seen:
>>>for line in open('script1.py'):# Use file iterators ...print(line.upper(), end='')... IMPORT SYS PRINT(SYS.PATH) X = 2 PRINT(2 ** 33)