标签云

微信群

扫码加入我们

WeChat QR Code

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/…

2019年05月22日15分52秒

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?

2019年05月22日15分52秒

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.

2019年05月22日15分52秒

fib is still a function. type(fib) = <type 'function'>.

2019年05月22日15分52秒