1

Can 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,1
    while True:
        yield b
        a,b = b, a + b
for i in fib():
    print(i)
  • This is platinum: http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python?rq=1 – sobolevn May 24 '15 at 10:24

4 Answers4

2

Any function that contains a yield will return a generator. The for-loop runs that generator to return values one at a time.

When you run:

for i in fib():
    print(i)

The actual mechanics of running the generator are:

_iterator = iter(fib())
while True:
    try:
        i = next(_iterator)
    except StopIteration:
        break
    print(i)

As you can see, the i variable is assigned the result of calling next() on the generator to get the next value.

Hope that makes it clear where the i comes from :-)

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
1

for just ranges over the vaue of the expression. If the expression calls a function, then its value is whatever is returned from the function, so the for ranges over the result of that function.

Note that here though fib is not a function, it is a generator. It successively yields the value of each step.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 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? –  May 24 '15 at 10:17
  • Quick Googling reveals [this](http://www.jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/) and [this](https://wiki.python.org/moin/Generators). 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. – Daniel Roseman May 24 '15 at 10:20
  • 1
    `fib` is still a function. `type(fib) = `. – sobolevn May 24 '15 at 10:21
0

To understand this you will have to understand what yield keyword does. Please take a look at this: What yield does?

Now you get an idea that fib() is not a function it is a generator. So in code:

def fib():
    a, b = 0,1
    while True:
        yield b    #from here value of b gets returned to the for statement
        a,b = b, a + b
for i in fib():
    print(i)

Since While never gets a false value. It keeps running.

Community
  • 1
  • 1
shaktimaan
  • 1,769
  • 13
  • 14
0

for loop generates disposable variable if you use it like above. For example, a list object is used again and again in a loop but a disposable iterator is deleted automatically after use.

And yield is a term like return which is used in functions. It gives a result and use it again in loop. Your codes give you the number known as fibonacci.

def fib():
    a, b = 0,1 #initially a=0 and b=1
    while True: #infinite loop term.
        yield b #generate b and use it again.
        a,b = b, a + b #a and b are now own their new values.

for i in fib(): #generate i using fib() function. i equals to b also thanks to yield term.
    print(i) #i think you known this
    if i>100:
        break #we have to stop loop because of yield.
Fatih1923
  • 2,665
  • 3
  • 21
  • 28