0

I'm using the answer(s) from here and here to check if my generator x is empty.

from itertools import tee
def my_generator():
    yield from range(100000000)
x = my_generator()
x, y = tee(x)
try:
    next(y)
except StopIteration:
    # x is empty do something
    quit()

What will happen to the elements extracted from x? can they be discarded ? or must be kept in memory for y?

# now consume x entirely
for z in x:
    print(x)
# how can y iterate over its objects ?
# will they have to reside in memory now ??
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 1
    What `y` are you referring to? Do you mean `z`? The elements "yielded" from the generator can be discarded after use. The generator is just a "lazy list" that you have to call every time you want an element from it. – Morten Jensen Oct 28 '21 at 13:00
  • @MortenJensen thanks, I fixed the typo - the problem is that since both x and y are now "dependent" on 'x', what happens when its elements are "gone"? – OrenIshShalom Oct 28 '21 at 14:20
  • Your post still refers to an `y` which confuses me. A generator is a sort of "function" that returns items one at a time. When you're done iterating through them, the generator is "spent" and you must re-create the generator to iterate over the item-collection again. Nothing happens to the collection itself, the generator is just an abstraction over the collection. – Morten Jensen Oct 28 '21 at 14:34

1 Answers1

1

tl;dr - elements yielded from x will be kept in memory until they are yielded from y as well.

When using itertools.tee, copies of all the yielded elements must be saved for the all the iterators returned from tee. In your case, those are x and y.

This is done to allow full iteration through both x and y.

If you look at the equivalent implementation in the Python docs for itertools.tee, you can see that all yielded values are saved until they are yielded from all the generators tee returned.

In your case, you'll have to consume both x and y or have them (both) go out of scope and garbage-collected for the items to be released.

tmr232
  • 1,171
  • 14
  • 23
  • 1
    I was testing in the python console and the objects are not destroyed even if they are yielded from all iterators. They only are destroyed when the iterators are deleted. I tried with gc.collect() – nadapez Oct 28 '21 at 15:01