Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the implementation detail for enumerate?

Python has enumerate() to iterate over objects with an index.I doubt that interpreters create a lot of int objects for the sole purpose of keeping track of where things are. The PEP page says the following, but I do not really understand what is going on under the hood:

It provides all iterables with the same advantage that iteritems() affords to dictionaries -- a compact, readable, reliable index notation.

So what is the magic here?

like image 211
Forethinker Avatar asked May 29 '13 07:05

Forethinker


1 Answers

enumerate() is an iterator; it only produces the index int value on the fly; it does not produce them all up front.

You can try to read the enumobject.c source code, but it basically can be translated to Python like this:

def enumerate(iterable, start=0):
    count = start
    for elem in iterable:
        yield count, elem
        count += 1

The yield keyword makes this a generator function, and you need to loop over the generator (or call next() on it) to advance the function to produce data, one yield call at a time.

Python also interns int values, all values between -5 and 256 (inclusive) are singletons, so the above code doesn't even produce new int objects until you reach 257.

like image 172
Martijn Pieters Avatar answered Sep 27 '22 21:09

Martijn Pieters