@@ -5,163 +5,180 @@ Last-Modified: $Date$
5
5
Author: nowonder@nowonder.de (Peter Schneider-Kamp)
6
6
Status: Deferred
7
7
Type: Standards Track
8
+ Content-Type: text/x-rst
8
9
Created: 22-Aug-2000
9
10
Python-Version: 2.1
10
11
Post-History:
11
12
12
13
13
14
Introduction
15
+ ============
14
16
15
- This PEP describes the often proposed feature of exposing the loop
16
- counter in for-loops. This PEP tracks the status and ownership of
17
- this feature. It contains a description of the feature and
18
- outlines changes necessary to support the feature. This PEP
19
- summarizes discussions held in mailing list forums, and provides
20
- URLs for further information, where appropriate. The CVS revision
21
- history of this file contains the definitive historical record.
17
+ This PEP describes the often proposed feature of exposing the loop
18
+ counter in for-loops. This PEP tracks the status and ownership of
19
+ this feature. It contains a description of the feature and
20
+ outlines changes necessary to support the feature. This PEP
21
+ summarizes discussions held in mailing list forums, and provides
22
+ URLs for further information, where appropriate. The CVS revision
23
+ history of this file contains the definitive historical record.
22
24
23
25
24
26
Motivation
27
+ ==========
25
28
26
- Standard for-loops in Python iterate over the elements of a
27
- sequence[1]. Often it is desirable to loop over the indices or
28
- both the elements and the indices instead.
29
+ Standard for-loops in Python iterate over the elements of a
30
+ sequence [1]_ . Often it is desirable to loop over the indices or
31
+ both the elements and the indices instead.
29
32
30
- The common idioms used to accomplish this are unintuitive. This
31
- PEP proposes two different ways of exposing the indices.
33
+ The common idioms used to accomplish this are unintuitive. This
34
+ PEP proposes two different ways of exposing the indices.
32
35
33
36
34
37
Loop counter iteration
38
+ ======================
35
39
36
- The current idiom for looping over the indices makes use of the
37
- built-in ' range' function:
40
+ The current idiom for looping over the indices makes use of the
41
+ built-in `` range`` function: :
38
42
39
- for i in range(len(sequence)):
40
- # work with index i
43
+ for i in range(len(sequence)):
44
+ # work with index i
41
45
42
- Looping over both elements and indices can be achieved either by the
43
- old idiom or by using the new ' zip' built-in function[2]:
46
+ Looping over both elements and indices can be achieved either by the
47
+ old idiom or by using the new `` zip`` built-in function [2]_: :
44
48
45
- for i in range(len(sequence)):
46
- e = sequence[i]
47
- # work with index i and element e
49
+ for i in range(len(sequence)):
50
+ e = sequence[i]
51
+ # work with index i and element e
48
52
49
- or
53
+ or::
50
54
51
- for i, e in zip(range(len(sequence)), sequence):
52
- # work with index i and element e
55
+ for i, e in zip(range(len(sequence)), sequence):
56
+ # work with index i and element e
53
57
54
58
55
59
The Proposed Solutions
60
+ ======================
56
61
57
- There are three solutions that have been discussed. One adds a
58
- non-reserved keyword, the other adds two built-in functions.
59
- A third solution adds methods to sequence objects.
62
+ There are three solutions that have been discussed. One adds a
63
+ non-reserved keyword, the other adds two built-in functions.
64
+ A third solution adds methods to sequence objects.
60
65
61
66
62
- Non-reserved keyword 'indexing'
67
+ Non-reserved keyword ``indexing``
68
+ =================================
63
69
64
- This solution would extend the syntax of the for-loop by adding
65
- an optional ' <variable> indexing' clause which can also be used
66
- instead of the ' <variable> in' clause. .
70
+ This solution would extend the syntax of the for-loop by adding
71
+ an optional `` <variable> indexing`` clause which can also be used
72
+ instead of the `` <variable> in`` clause.
67
73
68
- Looping over the indices of a sequence would thus become:
74
+ Looping over the indices of a sequence would thus become: :
69
75
70
- for i indexing sequence:
71
- # work with index i
76
+ for i indexing sequence:
77
+ # work with index i
72
78
73
- Looping over both indices and elements would similarly be:
79
+ Looping over both indices and elements would similarly be: :
74
80
75
- for i indexing e in sequence:
76
- # work with index i and element e
81
+ for i indexing e in sequence:
82
+ # work with index i and element e
77
83
78
84
79
- Built-in functions 'indices' and 'irange'
85
+ Built-in functions ``indices`` and ``irange``
86
+ =============================================
80
87
81
- This solution adds two built-in functions ' indices' and ' irange' .
82
- The semantics of these can be described as follows:
88
+ This solution adds two built-in functions `` indices`` and `` irange`` .
89
+ The semantics of these can be described as follows: :
83
90
84
- def indices(sequence):
85
- return range(len(sequence))
91
+ def indices(sequence):
92
+ return range(len(sequence))
86
93
87
- def irange(sequence):
88
- return zip(range(len(sequence)), sequence)
94
+ def irange(sequence):
95
+ return zip(range(len(sequence)), sequence)
89
96
90
- These functions could be implemented either eagerly or lazily and
91
- should be easy to extend in order to accept more than one sequence
92
- argument.
97
+ These functions could be implemented either eagerly or lazily and
98
+ should be easy to extend in order to accept more than one sequence
99
+ argument.
93
100
94
- The use of these functions would simplify the idioms for looping
95
- over the indices and over both elements and indices:
101
+ The use of these functions would simplify the idioms for looping
102
+ over the indices and over both elements and indices: :
96
103
97
- for i in indices(sequence):
98
- # work with index i
104
+ for i in indices(sequence):
105
+ # work with index i
99
106
100
- for i, e in irange(sequence):
101
- # work with index i and element e
107
+ for i, e in irange(sequence):
108
+ # work with index i and element e
102
109
103
110
104
111
Methods for sequence objects
112
+ ============================
105
113
106
- This solution proposes the addition of ' indices', ' items'
107
- and ' values' methods to sequences, which enable looping over
108
- indices only, both indices and elements, and elements only
109
- respectively.
114
+ This solution proposes the addition of `` indices``, `` items``
115
+ and `` values`` methods to sequences, which enable looping over
116
+ indices only, both indices and elements, and elements only
117
+ respectively.
110
118
111
- This would immensely simplify the idioms for looping over indices
112
- and for looping over both elements and indices:
119
+ This would immensely simplify the idioms for looping over indices
120
+ and for looping over both elements and indices: :
113
121
114
- for i in sequence.indices():
115
- # work with index i
122
+ for i in sequence.indices():
123
+ # work with index i
116
124
117
- for i, e in sequence.items():
118
- # work with index i and element e
125
+ for i, e in sequence.items():
126
+ # work with index i and element e
119
127
120
- Additionally it would allow to do looping over the elements
121
- of sequences and dicitionaries in a consistent way:
128
+ Additionally it would allow to do looping over the elements
129
+ of sequences and dictionaries in a consistent way: :
122
130
123
- for e in sequence_or_dict.values():
124
- # do something with element e
131
+ for e in sequence_or_dict.values():
132
+ # do something with element e
125
133
126
134
127
135
Implementations
136
+ ===============
128
137
129
- For all three solutions some more or less rough patches exist
130
- as patches at SourceForge:
138
+ For all three solutions some more or less rough patches exist
139
+ as patches at SourceForge:
131
140
132
- ' for i indexing a in l' : exposing the for-loop counter[3]
133
- add indices() and irange() to built-ins[4]
134
- add items() method to listobject[5]
141
+ - `` for i indexing a in l`` : exposing the for-loop counter [3]_
142
+ - add `` indices()`` and `` irange()`` to built-ins [4]_
143
+ - add `` items()`` method to listobject [5]_
135
144
136
- All of them have been pronounced on and rejected by the BDFL.
145
+ All of them have been pronounced on and rejected by the BDFL.
137
146
138
- Note that the ' indexing' keyword is only a NAME in the
139
- grammar and so does not hinder the general use of ' indexing' .
147
+ Note that the `` indexing`` keyword is only a `` NAME`` in the
148
+ grammar and so does not hinder the general use of `` indexing`` .
140
149
141
150
142
151
Backward Compatibility Issues
152
+ =============================
143
153
144
- As no keywords are added and the semantics of existing code
145
- remains unchanged, all three solutions can be implemented
146
- without breaking existing code.
154
+ As no keywords are added and the semantics of existing code
155
+ remains unchanged, all three solutions can be implemented
156
+ without breaking existing code.
147
157
148
158
149
159
Copyright
160
+ =========
150
161
151
- This document has been placed in the public domain.
162
+ This document has been placed in the public domain.
152
163
153
164
154
165
References
166
+ ==========
155
167
156
- [1] http://docs.python.org/reference/compound_stmts.html#for
157
- [2] Lockstep Iteration, PEP 201
158
- [3] http://sourceforge.net/patch/download.php?id=101138
159
- [4] http://sourceforge.net/patch/download.php?id=101129
160
- [5] http://sourceforge.net/patch/download.php?id=101178
168
+ .. [1] http://docs.python.org/reference/compound_stmts.html#for
161
169
170
+ .. [2] Lockstep Iteration, PEP 201
162
171
163
-
164
- Local Variables:
165
- mode: indented-text
166
- indent-tabs-mode: nil
167
- End:
172
+ .. [3] http://sourceforge.net/patch/download.php?id=101138
173
+
174
+ .. [4] http://sourceforge.net/patch/download.php?id=101129
175
+
176
+ .. [5] http://sourceforge.net/patch/download.php?id=101178
177
+
178
+
179
+
180
+ ..
181
+ Local Variables:
182
+ mode: indented-text
183
+ indent-tabs-mode: nil
184
+ End:
0 commit comments