This question already has an answer here:What does the “yield” keyword do?42 answersCan someone explain what each step in this does? I have never seen "for i in X:" used where X is a generator, and I am failing to understand how the i interacts with the function if it's not being inserted between the (). def fib():a, b = 0,1while True:yield ba,b = b, a + bfor i in fib():print(i)
This is platinum: stackoverflow.com/questions/231767/…
I did not know that a function was not a generator. Is there a link you can point me to that explains the difference between them?
Quick Googling reveals this and this. That was more a subsidiary point though: the code would work just as well if fib was a normal function that returns [1, 2, 3, 4], the for would still iterate through it.
fib is still a function. type(fib) = <type 'function'>.