getting an index in a for loop

Beni Cherniavsky cben at techunix.technion.ac.il
Sun Feb 2 10:00:14 EST 2003


On 2003-01-31, Erik Max Francis wrote:

> However, enumerate is still only available in 2.3 so it's not really
> widely available yet.
>
I'm one of those who like to use all the lastest Python features (because
they tend to be so good <wink>).  Therefore I always favor the
back-porting approach and in this case, a trivial Python implementation
can be just written in his code, until 2.3 becomes common place.  Is there
anything against back-porting this to 2.2, at least as pure-Python?
Adding a builtin should break no code, right?  Actually, as I'm
writing this, I've just stuck this into my sitecustimize.py (-::

from __future__ import generators
import __builtin__

try:
    enumerate
except:
    def enumerate(iterable):
        """Back port of the 2.3 builtin."""
        i = 0
        for item in iterable:
            yield (i, item)
            i += 1
    __builtin__.enumerate = enumerate

In user code, omit the last line.

-- 
Beni Cherniavsky <cben at tx.technion.ac.il>

My first thought upon hearing the company name "22 Design":
That's not a valid identifier (starts with a digit)!








More information about the Python-list mailing list