I came across some code like this:
def func(tree):
nodes = tree
for node in nodes:
yield node
nodes += [42]
gen = func([-42, 3, 1, 4, 159])
for i in range(10):
print(next(gen))
Does this code have undefined behaviour?
Specifically, does:
for node in nodes:
yield node
nodes += [42]
show undefined behviour?
I know that:
for node in nodes:
# yield node
nodes += [42]
may cause unexpected behaviour because the variable we are iterating through (nodes
) is being updated in the for loop.