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:
parent
1ffce41fee
commit
516a1a8b52
66
pep-0201.txt
66
pep-0201.txt
|
@ -1,12 +1,13 @@
|
||||||
PEP: 201
|
PEP: 201
|
||||||
Title: Parallel Iteration
|
Title: Parallel Iteration
|
||||||
Version: $Revision$
|
Version: $Revision$
|
||||||
Owner: bwarsaw@beopen.com (Barry A. Warsaw)
|
Author: bwarsaw@beopen.com (Barry A. Warsaw)
|
||||||
Python-Version: 2.0
|
Python-Version: 2.0
|
||||||
Status: Draft
|
Status: Draft
|
||||||
|
Created: 13-Jul-2000
|
||||||
|
Post-History:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Introduction
|
Introduction
|
||||||
|
|
||||||
This PEP describes the `parallel iteration' proposal for Python
|
This PEP describes the `parallel iteration' proposal for Python
|
||||||
|
@ -19,42 +20,19 @@ Introduction
|
||||||
history of this file contains the definitive historical record.
|
history of this file contains the definitive historical record.
|
||||||
|
|
||||||
|
|
||||||
|
Motivation
|
||||||
Standard For-Loops
|
|
||||||
|
|
||||||
Motivation for this feature has its roots in a concept described
|
Standard for-loops in Python iterate over every element in a
|
||||||
as `parallel for loops'. A standard for-loop in Python iterates
|
sequence until the sequence is exhausted[1]. However, for-loops
|
||||||
over every element in the sequence until the sequence is
|
iterate over only a single sequence, and it is often desirable to
|
||||||
exhausted. A `break' statement inside the loop suite causes an
|
loop over more than one sequence, in a lock-step, "Chinese Menu"
|
||||||
explicit loop exit. For-loops also have else: clauses which get
|
type of way.
|
||||||
executed when the loop exits normally (i.e. not by execution of a
|
|
||||||
break).
|
|
||||||
|
|
||||||
For-loops can iterate over built-in types such as lists and
|
The common idioms used to accomplish this are unintuitive and
|
||||||
tuples, but they can also iterate over instance types that conform
|
inflexible. This PEP proposes a standard way of performing such
|
||||||
to an informal sequence protocol. This protocol states that the
|
iterations by introducing a new builtin function called `zip'.
|
||||||
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.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Parallel For-Loops
|
Parallel For-Loops
|
||||||
|
|
||||||
Parallel for-loops are non-nested iterations over two or more
|
Parallel for-loops are non-nested iterations over two or more
|
||||||
|
@ -70,12 +48,6 @@ Parallel For-Loops
|
||||||
(1, 4)
|
(1, 4)
|
||||||
(2, 5)
|
(2, 5)
|
||||||
(3, 6)
|
(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)
|
>>> map(None, a, b)
|
||||||
[(1, 4), (2, 5), (3, 6)]
|
[(1, 4), (2, 5), (3, 6)]
|
||||||
|
|
||||||
|
@ -106,7 +78,6 @@ Parallel For-Loops
|
||||||
comprehensions' (see pep-0202.txt).
|
comprehensions' (see pep-0202.txt).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The Proposed Solution
|
The Proposed Solution
|
||||||
|
|
||||||
The proposed solution is to introduce a new built-in sequence
|
The proposed solution is to introduce a new built-in sequence
|
||||||
|
@ -129,7 +100,6 @@ The Proposed Solution
|
||||||
Issues below for more discussion.
|
Issues below for more discussion.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Lazy Execution
|
Lazy Execution
|
||||||
|
|
||||||
For performance purposes, zip() does not construct the list of
|
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
|
for-loop protocol. This method constructs the individual tuples
|
||||||
on demand.
|
on demand.
|
||||||
|
|
||||||
|
Guido is strongly opposed to lazy execution. See Open Issues.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
|
|
||||||
Here are some examples, based on the reference implementation
|
Here are some examples, based on the reference implementation
|
||||||
|
@ -194,7 +165,6 @@ Examples
|
||||||
not all the same length.
|
not all the same length.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference Implementation
|
Reference Implementation
|
||||||
|
|
||||||
Here is a reference implementation, in Python of the zip()
|
Here is a reference implementation, in Python of the zip()
|
||||||
|
@ -292,7 +262,6 @@ Reference Implementation
|
||||||
return _Zipper(args, kws)
|
return _Zipper(args, kws)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Rejected Elaborations
|
Rejected Elaborations
|
||||||
|
|
||||||
Some people have suggested that the user be able to specify the
|
Some people have suggested that the user be able to specify the
|
||||||
|
@ -352,9 +321,12 @@ Rejected Elaborations
|
||||||
advantages.
|
advantages.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Open Issues
|
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
|
- What should "zip(a)" do? Given
|
||||||
|
|
||||||
a = (1, 2, 3); zip(a)
|
a = (1, 2, 3); zip(a)
|
||||||
|
@ -427,7 +399,6 @@ Open Issues
|
||||||
always override pad if both were given.
|
always override pad if both were given.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
References
|
References
|
||||||
|
|
||||||
[1] http://www.python.org/doc/devel/ref/for.html
|
[1] http://www.python.org/doc/devel/ref/for.html
|
||||||
|
@ -435,7 +406,6 @@ References
|
||||||
|
|
||||||
TBD: URL to python-dev archives
|
TBD: URL to python-dev archives
|
||||||
|
|
||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
|
|
||||||
This document has been placed in the public domain.
|
This document has been placed in the public domain.
|
||||||
|
|
Loading…
Reference in New Issue