0

Can someone explain how the increment of a or value of a occurs within the for loop to generate the Fib sequence? I have an understanding of (a, b = b, a + b). However, I am unable to figure how the increment occurs in the for loop when next() is called.

 def fib(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b
x = fib(4)
print(x.__next__())
print(x.__next__())
print(x.__next__())
print(x.__next__())

0 1 1 2

RSSregex
  • 179
  • 7
  • 1
    `b` is incremented by `a`, and `a` is assigned to `b` n the next iteration. Could you elaborate on what you are confused about? – iz_ Apr 17 '19 at 01:51
  • Every time you run `__next__` on a generator, it runs until the next `yield`. `__next__` returns that value and the generator function gets paused there. – Ry- Apr 17 '19 at 01:52
  • Your question seems to be a basic question about how generators work and doesn't really involve `a,b = b, a+b`. Think of `yield` as sort of like `print`. If you would have no problem understanding how the code would work with `print()` then you shouldn't have any problem with `yield`: `print` prints to the screen, `yield` "prints" to the future. `__next__` is just a way of seeing the next value "printed" – John Coleman Apr 17 '19 at 01:53
  • when you use `yield` instead of `return` then it create generator. When you run it with next() then it run till `yield`, and it exits (like `return`) but when you run it again then it start at `yield` so it continue loop, it not start at the beginning of function. – furas Apr 17 '19 at 01:54
  • I get the yield/next part. My question is for n=1,2,3. How does `a` increment to generate the Fib sequence in (a,b = b, a+b) – RSSregex Apr 17 '19 at 02:00
  • 1
    If you wonder how values of `a` and `b` are passed from one step to the next, then I would agree with @JohnColeman and feel that your understanding of this is related to [the more general question about generators](https://stackoverflow.com/questions/25232350/how-generators-work-in-python). – cglacet Apr 17 '19 at 02:01
  • “I have an understanding of (a, b = b, a + b)” – so this is not accurate? – Ry- Apr 17 '19 at 02:01
  • To the best of my understanding as explained (a, b = b, a + b) in some other post. Meaning the expressions on the right are evaluated first. – RSSregex Apr 17 '19 at 02:08
  • Even if that's a duplicate, I think the other question would deserve a second answer on an higher level of abstraction ([the current answer](https://stackoverflow.com/a/25233077/1720199) is very technical and go into implementation details that might not be of interest for everyone). – cglacet Apr 17 '19 at 02:38
  • There is another duplicate of this question. On this one I feel like both the [accepted answer](https://stackoverflow.com/a/1756156/1720199) and [this answer](https://stackoverflow.com/a/1932598/1720199) are reasonable answers to your question. – cglacet Apr 17 '19 at 03:11

1 Answers1

1

To begin with, you can go to the next element of the generator by next(x).

Just using a print statement in your code will help you understand as well.

def fib(n):
    a, b = 0, 1
    for _ in range(n):
        print(a, b)
        yield a
        a, b = b, a + b

x = fib(4)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
0 1
0
1 1
1
1 2
1
2 3
2

Here the next function lazily evaluates and prints out the value of a, until you call next again. So in the first next, it prints out 0.
Then when you call next again, a = 1 and b = 1, and you get a = 1.
Then when you call next again, a = 1 and b = 2, and you get a = 1.
Then when you call next again, a = 2 and b = 3, and you get a = 2.
After that, since you are done with your for loop, you cannot call next anymore

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40