Online Book Reader

Home Category

Learning Python - Mark Lutz [202]

By Root 1595 0
that Python provides additional tools to make it simpler and more efficient. This chapter begins our exploration of these tools. Specifically, it presents the related concepts of Python’s iteration protocol—a method-call model used by the for loop—and fills in some details on list comprehensions—a close cousin to the for loop that applies an expression to items in an iterable.

Because both of these tools are related to both the for loop and functions, we’ll take a two-pass approach to covering them in this book: this chapter introduces the basics in the context of looping tools, serving as something of continuation of the prior chapter, and a later chapter (Chapter 20) revisits them in the context of function-based tools. In this chapter, we’ll also sample additional iteration tools in Python and touch on the new iterators available in Python 3.0.

One note up front: some of the concepts presented in these chapters may seem advanced at first glance. With practice, though, you’ll find that these tools are useful and powerful. Although never strictly required, because they’ve become commonplace in Python code, a basic understanding can also help if you must read programs written by others.

Iterators: A First Look

In the preceding chapter, I mentioned that the for loop can work on any sequence type in Python, including lists, tuples, and strings, like this:

>>> for x in [1, 2, 3, 4]: print(x ** 2, end=' ')

...

1 4 9 16

>>> for x in (1, 2, 3, 4): print(x ** 3, end=' ')

...

1 8 27 64

>>> for x in 'spam': print(x * 2, end=' ')

...

ss pp aa mm

Actually, the for loop turns out to be even more generic than this—it works on any iterable object. In fact, this is true of all iteration tools that scan objects from left to right in Python, including for loops, the list comprehensions we’ll study in this chapter, in membership tests, the map built-in function, and more.

The concept of “iterable objects” is relatively recent in Python, but it has come to permeate the language’s design. It’s essentially a generalization of the notion of sequences—an object is considered iterable if it is either a physically stored sequence or an object that produces one result at a time in the context of an iteration tool like a for loop. In a sense, iterable objects include both physical sequences and virtual sequences computed on demand.[33]

The Iteration Protocol: File Iterators

One of the easiest ways to understand what this means is to look at how it works with a built-in type such as the file. Recall from Chapter 9 that open file objects have a method called readline, which reads one line of text from a file at a time—each time we call the readline method, we advance to the next line. At the end of the file, an empty string is returned, which we can detect to break out of the loop:

>>> f = open('script1.py') # Read a 4-line script file in this directory

>>> f.readline() # readline loads one line on each call

'import sys\n'

>>> f.readline()

'print(sys.path)\n'

>>> f.readline()

'x = 2\n'

>>> f.readline()

'print(2 ** 33)\n'

>>> f.readline() # Returns empty string at end-of-file

''

However, files also have a method named __next__ that has a nearly identical effect—it returns the next line from a file each time it is called. The only noticeable difference is that __next__ raises a built-in StopIteration exception at end-of-file instead of returning an empty string:

>>> f = open('script1.py') # __next__ loads one line on each call too

>>> f.__next__() # But raises an exception at end-of-file

'import sys\n'

>>> f.__next__()

'print(sys.path)\n'

>>> f.__next__()

'x = 2\n'

>>> f.__next__()

'print(2 ** 33)\n'

>>> f.__next__()

Traceback (most recent call last):

...more exception text omitted...

StopIteration

This interface is exactly what we call the iteration protocol in Python. Any object with a __next__ method to advance to a next result, which raises StopIteration at the end of the series of results, is considered iterable in Python. Any such object may also be stepped through with a for loop

Return Main Page Previous Page Next Page

®Online Book Reader