Forethinker Forethinker - 2 months ago 15
Python Question

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?

Answer Source

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.