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.