python-peps/pep-0212.txt

183 lines
4.9 KiB
Plaintext
Raw Normal View History

PEP: 212
Title: Loop Counter Iteration
Author: Peter Schneider-Kamp <nowonder@nowonder.de>
Status: Rejected
Type: Standards Track
2017-01-19 13:00:30 -05:00
Content-Type: text/x-rst
Created: 22-Aug-2000
Python-Version: 2.1
Post-History:
Rejection Notice
================
This PEP has been rejected. ``enumerate()``, introduced in :pep:`279`,
covers the use-case proposed in this PEP, and the PEP author has been
unreachable.
Introduction
2017-01-19 13:00:30 -05:00
============
2017-01-19 13:00:30 -05:00
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.
Motivation
2017-01-19 13:00:30 -05:00
==========
2017-01-19 13:00:30 -05:00
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.
2017-01-19 13:00:30 -05:00
The common idioms used to accomplish this are unintuitive. This
PEP proposes two different ways of exposing the indices.
Loop counter iteration
2017-01-19 13:00:30 -05:00
======================
2017-01-19 13:00:30 -05:00
The current idiom for looping over the indices makes use of the
built-in ``range`` function::
2017-01-19 13:00:30 -05:00
for i in range(len(sequence)):
# work with index i
2017-01-19 13:00:30 -05:00
Looping over both elements and indices can be achieved either by the
old idiom or by using the new ``zip`` built-in function [2]_::
2017-01-19 13:00:30 -05:00
for i in range(len(sequence)):
e = sequence[i]
# work with index i and element e
2017-01-19 13:00:30 -05:00
or::
2017-01-19 13:00:30 -05:00
for i, e in zip(range(len(sequence)), sequence):
# work with index i and element e
The Proposed Solutions
2017-01-19 13:00:30 -05:00
======================
2017-01-19 13:00:30 -05:00
There are three solutions that have been discussed. One adds a
non-reserved keyword, the other adds two built-in functions.
A third solution adds methods to sequence objects.
2017-01-19 13:00:30 -05:00
Non-reserved keyword ``indexing``
=================================
2017-01-19 13:00:30 -05:00
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.
2017-01-19 13:00:30 -05:00
Looping over the indices of a sequence would thus become::
2017-01-19 13:00:30 -05:00
for i indexing sequence:
# work with index i
2017-01-19 13:00:30 -05:00
Looping over both indices and elements would similarly be::
2017-01-19 13:00:30 -05:00
for i indexing e in sequence:
# work with index i and element e
2017-01-19 13:00:30 -05:00
Built-in functions ``indices`` and ``irange``
=============================================
2017-01-19 13:00:30 -05:00
This solution adds two built-in functions ``indices`` and ``irange``.
The semantics of these can be described as follows::
2017-01-19 13:00:30 -05:00
def indices(sequence):
return range(len(sequence))
2017-01-19 13:00:30 -05:00
def irange(sequence):
return zip(range(len(sequence)), sequence)
2017-01-19 13:00:30 -05:00
These functions could be implemented either eagerly or lazily and
should be easy to extend in order to accept more than one sequence
argument.
2017-01-19 13:00:30 -05:00
The use of these functions would simplify the idioms for looping
over the indices and over both elements and indices::
2017-01-19 13:00:30 -05:00
for i in indices(sequence):
# work with index i
2017-01-19 13:00:30 -05:00
for i, e in irange(sequence):
# work with index i and element e
Methods for sequence objects
2017-01-19 13:00:30 -05:00
============================
2017-01-19 13:00:30 -05:00
This solution proposes the addition of ``indices``, ``items``
and ``values`` methods to sequences, which enable looping over
indices only, both indices and elements, and elements only
respectively.
2017-01-19 13:00:30 -05:00
This would immensely simplify the idioms for looping over indices
and for looping over both elements and indices::
2017-01-19 13:00:30 -05:00
for i in sequence.indices():
# work with index i
2017-01-19 13:00:30 -05:00
for i, e in sequence.items():
# work with index i and element e
2017-01-19 13:00:30 -05:00
Additionally it would allow to do looping over the elements
of sequences and dictionaries in a consistent way::
2017-01-19 13:00:30 -05:00
for e in sequence_or_dict.values():
# do something with element e
Implementations
2017-01-19 13:00:30 -05:00
===============
2017-01-19 13:00:30 -05:00
For all three solutions some more or less rough patches exist
as patches at SourceForge:
2017-01-19 13:00:30 -05:00
- ``for i indexing a in l``: exposing the for-loop counter [3]_
- add ``indices()`` and ``irange()`` to built-ins [4]_
- add ``items()`` method to listobject [5]_
2017-01-19 13:00:30 -05:00
All of them have been pronounced on and rejected by the BDFL.
2017-01-19 13:00:30 -05:00
Note that the ``indexing`` keyword is only a ``NAME`` in the
grammar and so does not hinder the general use of ``indexing``.
Backward Compatibility Issues
2017-01-19 13:00:30 -05:00
=============================
2017-01-19 13:00:30 -05:00
As no keywords are added and the semantics of existing code
remains unchanged, all three solutions can be implemented
without breaking existing code.
Copyright
2017-01-19 13:00:30 -05:00
=========
2017-01-19 13:00:30 -05:00
This document has been placed in the public domain.
References
2017-01-19 13:00:30 -05:00
==========
2017-01-19 13:00:30 -05:00
.. [1] http://docs.python.org/reference/compound_stmts.html#for
.. [2] Lockstep Iteration, :pep:`201`
2017-01-19 13:00:30 -05:00
.. [3] http://sourceforge.net/patch/download.php?id=101138
.. [4] http://sourceforge.net/patch/download.php?id=101129
.. [5] http://sourceforge.net/patch/download.php?id=101178