[Python-Dev] Lockstep iteration - eureka! (original) (raw)

Just van Rossum just@letterror.com
Wed, 9 Aug 2000 16:01:18 +0100


At 7:49 AM -0500 09-08-2000, Guido van Rossum wrote:

In the past there were syntax proposals, e.g. ``for i indexing s''. Maybe you and Just can draft a PEP?

PEP: 1716099-3 Title: Index-enhanced sequence iteration Version: Revision:1.1Revision: 1.1 Revision:1.1 Owner: Someone-with-commit-rights Python-Version: 2.0 Status: Incomplete

Introduction

This PEP proposes a way to more conveniently iterate over a
sequence and its indices.

Features

It adds an optional clause to the 'for' statement:

    for <index> indexing <element> in <seq>:
        ...

This is equivalent to (see the zip() PEP):

    for <index>, <element> in zip(range(len(seq)), seq):
        ...

Except no new list is created.

Mechanism

The index of the current element in a for-loop already
exists in the implementation, however, it is not reachable
from Python. The new 'indexing' keyword merely exposes the
internal counter.

Implementation

Implementation should be trivial for anyone named Guido,
Tim or Thomas. Justs better not try.

Advantages:

Less code needed for this common operation, which is
currently most often written as:

    for index in range(len(seq)):
        element = seq[i]
        ...

Disadvantages:

It will break that one person's code that uses "indexing"
as a variable name.

Copyright

This document has been placed in the public domain.

Local Variables: mode: indented-text indent-tabs-mode: nil End: