In a compromise with JHy, and to be more consistent with the style now

documented in PEP1, remove the Emacs page breaks.

Also, Owner: -> Author:, added Created: and Post-History: headers

Changed "Standard For-Loops" section to "Motivation" and shortened
considerably; readers already know how for-loops work in Python.

Added notes about Guido's veto of lazy evaluation.
This commit is contained in:
Barry Warsaw 2000-07-25 21:51:55 +00:00
parent 1ffce41fee
commit 516a1a8b52
1 changed files with 18 additions and 48 deletions

View File

@ -1,12 +1,13 @@
PEP: 201
Title: Parallel Iteration
Version: $Revision$
Owner: bwarsaw@beopen.com (Barry A. Warsaw)
Author: bwarsaw@beopen.com (Barry A. Warsaw)
Python-Version: 2.0
Status: Draft
Created: 13-Jul-2000
Post-History:
Introduction
This PEP describes the `parallel iteration' proposal for Python
@ -19,42 +20,19 @@ Introduction
history of this file contains the definitive historical record.
Standard For-Loops
Motivation
Motivation for this feature has its roots in a concept described
as `parallel for loops'. A standard for-loop in Python iterates
over every element in the sequence until the sequence is
exhausted. A `break' statement inside the loop suite causes an
explicit loop exit. For-loops also have else: clauses which get
executed when the loop exits normally (i.e. not by execution of a
break).
Standard for-loops in Python iterate over every element in a
sequence until the sequence is exhausted[1]. However, for-loops
iterate over only a single sequence, and it is often desirable to
loop over more than one sequence, in a lock-step, "Chinese Menu"
type of way.
For-loops can iterate over built-in types such as lists and
tuples, but they can also iterate over instance types that conform
to an informal sequence protocol. This protocol states that the
instance should implement the __getitem__() method, expecting a
monotonically increasing index starting at 0, and this method
should raise an IndexError when the sequence is exhausted. This
protocol is currently undocumented -- a defect in Python's
documentation hopefully soon corrected.
For-loops are described in the Python language reference
manual[1].
An example for-loop:
>>> for i in (1, 2, 3): print i
...
1
2
3
In this example, the variable `i' is called the `target', and is
assigned the next element of the list, each time through the loop.
The common idioms used to accomplish this are unintuitive and
inflexible. This PEP proposes a standard way of performing such
iterations by introducing a new builtin function called `zip'.
Parallel For-Loops
Parallel for-loops are non-nested iterations over two or more
@ -70,12 +48,6 @@ Parallel For-Loops
(1, 4)
(2, 5)
(3, 6)
Here, map() returns a list of N-tuples, where N is the number of
sequences in map()'s argument list (after the initial `None').
Each tuple is constructed of the i-th elements from each of the
argument lists, specifically in this example:
>>> map(None, a, b)
[(1, 4), (2, 5), (3, 6)]
@ -106,7 +78,6 @@ Parallel For-Loops
comprehensions' (see pep-0202.txt).
The Proposed Solution
The proposed solution is to introduce a new built-in sequence
@ -129,7 +100,6 @@ The Proposed Solution
Issues below for more discussion.
Lazy Execution
For performance purposes, zip() does not construct the list of
@ -138,8 +108,9 @@ Lazy Execution
for-loop protocol. This method constructs the individual tuples
on demand.
Guido is strongly opposed to lazy execution. See Open Issues.
Examples
Here are some examples, based on the reference implementation
@ -194,7 +165,6 @@ Examples
not all the same length.
Reference Implementation
Here is a reference implementation, in Python of the zip()
@ -292,7 +262,6 @@ Reference Implementation
return _Zipper(args, kws)
Rejected Elaborations
Some people have suggested that the user be able to specify the
@ -352,9 +321,12 @@ Rejected Elaborations
advantages.
Open Issues
- Guido opposes lazy evaluation for zip(). He believes zip()
should return a real list, with an xzip() lazy evaluator added
later if necessary.
- What should "zip(a)" do? Given
a = (1, 2, 3); zip(a)
@ -427,7 +399,6 @@ Open Issues
always override pad if both were given.
References
[1] http://www.python.org/doc/devel/ref/for.html
@ -435,7 +406,6 @@ References
TBD: URL to python-dev archives
Copyright
This document has been placed in the public domain.