I have simple program that I am using to understand how generators/yeild works.
def learn_yield(numbers):
for i in numbers:
yield (i*i)
numbers = [5]
sq = learn_yield(numbers)
numbers.append(6)
for i in sq:
print (i)
Here's where my understanding is unclear:
The append method is called after the call to the learn_yield function. I would have expected the output of print(i) as 25 and not
25 36
How exactly did the number 6 get sent to the function?
If I move numbers.append(6) to after the for loop, then I get the behavior I think should happen in the first place. Does this mean that the call to the function is made again when the loop is iterating?
System - PC, Windows 10 Python - 3.7 Sublime Text