PEP 212 is hijacked, and renamed "Loop Counter Iteration". It is
assigned to Peter Schneider-Kamp. Based on recent discussions with the BDFL and the Python 2.0 release manager, this PEP is deferred until Python 2.1. Some minor editorial and formating modifications were performed on the text supplied by Peter. Editor also added footnotes.
This commit is contained in:
parent
5ff3374dd9
commit
1a3d71472f
119
pep-0212.txt
119
pep-0212.txt
|
@ -1,44 +1,119 @@
|
|||
PEP: 212
|
||||
Title: Additional Builtin Generators
|
||||
Title: Loop Counter Iteration
|
||||
Version: $Revision$
|
||||
Owner: bwarsaw@beopen.com (Barry A. Warsaw)
|
||||
Python-Version: 2.0
|
||||
Status: Incomplete
|
||||
Author: nowonder@nowonder.de (Peter Schneider-Kamp)
|
||||
Status: Draft
|
||||
Type: Standards Track
|
||||
Python-Version: 2.1
|
||||
Created: 22-Aug-2000
|
||||
|
||||
|
||||
|
||||
Introduction
|
||||
|
||||
This PEP describes some proposed additional generator-creating
|
||||
builtin functions for Python 2.0. This PEP tracks the status and
|
||||
ownership of this feature, slated for introduction in Python 2.0.
|
||||
It contains a description of the feature and outlines changes
|
||||
necessary to support the feature. This PEP summarizes discussions
|
||||
held in mailing list forums, and provides URLs for further
|
||||
information, where appropriate. The CVS revision history of this
|
||||
file contains the definitive historical record.
|
||||
This PEP describes the often proposed feature of exposing the loop
|
||||
counter in for-loops. This PEP tracks the status and ownership of
|
||||
this feature. It contains a description of the feature and
|
||||
outlines changes necessary to support the feature. This PEP
|
||||
summarizes discussions held in mailing list forums, and provides
|
||||
URLs for further information, where appropriate. The CVS revision
|
||||
history of this file contains the definitive historical record.
|
||||
|
||||
|
||||
|
||||
New functions
|
||||
Motivation
|
||||
|
||||
irange()
|
||||
tuples()
|
||||
lists()
|
||||
dict()
|
||||
Standard for-loops in Python iterate over the elements of a
|
||||
sequence[1]. Often it is desirable to loop over the indices or
|
||||
both the elements and the indices instead.
|
||||
|
||||
The common idioms used to accomplish this are unintuitive. This
|
||||
PEP proposes two different ways of exposing the indices.
|
||||
|
||||
|
||||
|
||||
Reference Implementation
|
||||
Loop counter iteration
|
||||
|
||||
The current idiom for looping over the indices makes use of the
|
||||
built-in 'range' function:
|
||||
|
||||
for i in range(len(sequence)):
|
||||
# work with index i
|
||||
|
||||
Looping over both elements and indices can be achieved either by the
|
||||
old idiom or by using the new 'zip' built-in function[2]:
|
||||
|
||||
for i in range(len(sequence)):
|
||||
e = sequence[i]
|
||||
# work with index i and element e
|
||||
|
||||
or
|
||||
|
||||
for i, e in zip(range(len(sequence)), sequence):
|
||||
# work with index i and element e
|
||||
|
||||
|
||||
The Proposed Solutions
|
||||
|
||||
There are two solutions that have been discussed. One adds a
|
||||
non-reserved keyword, the other adds two built-in functions.
|
||||
|
||||
A third solution would have been the addition of 'keys', 'items'
|
||||
and 'values' methods to sequences, which enable looping over
|
||||
indices only, both indices and elements, and elements only
|
||||
respectively.
|
||||
|
||||
|
||||
Non-reserved keyword 'indexing'
|
||||
|
||||
This solution would extend the syntax of the for-loop by adding
|
||||
an optional '<variable> indexing' clause which can also be used
|
||||
instead of the '<variable> in' clause..
|
||||
|
||||
Looping over the indices of a sequence would thus become:
|
||||
|
||||
for i indexing sequence:
|
||||
# work with index i
|
||||
|
||||
Looping over both indices and elements would similarly be:
|
||||
|
||||
for i indexing e in sequence:
|
||||
# work with index i and element e
|
||||
|
||||
|
||||
Built-in functions 'indices' and 'irange'
|
||||
|
||||
This solution adds two built-in functions 'indices' and 'irange'.
|
||||
The semantics of these can be described as follows:
|
||||
|
||||
def indices(sequence):
|
||||
return range(len(sequence))
|
||||
|
||||
def irange(sequence):
|
||||
return zip(range(len(sequence)), sequence)
|
||||
|
||||
These functions could be implemented either eagerly or lazily and
|
||||
should be easy to extend in order to accept more than one sequence
|
||||
argument.
|
||||
|
||||
The use of these functions would simplify the idioms for looping
|
||||
over the indices and over both elements and indices:
|
||||
|
||||
for i in indices(sequence):
|
||||
# work with index i
|
||||
|
||||
for i, e in irange(sequence):
|
||||
# work with index i and element e
|
||||
|
||||
|
||||
|
||||
Copyright
|
||||
|
||||
This document has been placed in the public domain.
|
||||
|
||||
|
||||
References
|
||||
|
||||
[1] http://www.python.org/doc/current/ref/for.html
|
||||
[2] Lockstep Iteration, pep-0201.txt
|
||||
|
||||
|
||||
|
||||
Local Variables:
|
||||
mode: indented-text
|
||||
|
|
Loading…
Reference in New Issue