I'm learning about generators, so I defined the following function which calculates Fibonacci sequence:
def fib(max):
a, b = 0, 1
while a < max:
yield a
a, b = b, a + b
I tried to use it like this, but it didn't work:
next(fib(10))
Out[413]: 0
next(fib(10))
Out[414]: 0
next(fib(10))
Out[415]: 0
However, using it like this works as expected:
f = fib(10)
next(f)
Out[417]: 0
next(f)
Out[418]: 1
next(f)
Out[419]: 1
next(f)
Out[420]: 2
Why the first case does not work?