What is the behavior of .close()
on a generator that has just started?
def gen():
while True:
yield 1
g = gen()
g.send(1)
throws TypeError: can't send non-None value to a just-started generator
def gen():
while True:
try:
yield 1
except GeneratorExit:
print("exit")
raise
g = gen()
next(g)
g.close()
prints exit
But what is the behavior of:
def gen():
while True:
try:
yield 1
except GeneratorExit:
print("exit")
raise
g = gen()
g.close()
and how can I check it?
EDIT:
When I try it, nothing happens, but subsequent calls to next(g)
raise StopIteration
.
My question is actually: what happens in terms of memory, but I guess most is freed.