Where are the 'elements' being stored in a generator?
A generator
do not store the values, you need to think of a generator as a function with context, it will save it state and GENERATE
the values each time it is asked to do so, so, it gives you a value, then "discard" it, hold the context of the computation and wait till you ask for more; and will do so until the generator context is exhausted.
def firstn(n):
num = 0
while num < n:
yield num
num += 1
In this example you provide, the "only" memory used is num
, is where the computation is stored, the firstn
generator holds the num
in its context
till the while loop
is finised.