SoFunction
Updated on 2024-11-19

Python generator of the yield details

yield

To clarify the difference between yield and return, let's look at a simple example:

>>> def self_return(n):

...    print('rocky')

...    while n > 0:

...            print('before return')

...            return n

...            n -= 1

...            print('after return')

...

>>> s = self_return(3)

rocky

before return

>>> s

3

From the above example of self_return(n) function is called in the process we can clearly see, s = self_return(3) function of the body of the statement began to execute, encountered after the return return the value of the return, and the end of the function in the body of the execution, so we see the result is the result of return after the statement is not executed at all, this is the This is a feature of return. Do you remember it? If not, you can go back to my previous article.

Let's try it again by replacing return with yield:

>>> def self_yield(n):

...    print('rocky')

...    while n > 0:

...            print('before yield')

...            yield n

...            n -= 1

...            print('after yield')

...

>>> s = self_yield(3)

>>> s.__next__()

rocky

before yield

3

Looking closely at the above example you'll see that s = self_yield(n) doesn't bother to execute the statement inside the function and s.next() will return the value and pause when it encounters a yield. Let's go ahead and try it again:

>>> s.__next__()

after yield

before yield

2

>>> s.__next__()

after yield

before yield

1

>>> s.__next__()

after yield

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

StopIteration

By continuing the operation above, we can see that each time we encounter yield will return the value and pause, the next time the execution of the last pause from the location of the start and continue the execution, when there is no value that meets the conditions, an exception will be thrown.

Combined with the above analysis and the results of the execution of the use case, I believe you have you have understood the characteristics of yield, but also know the difference between it and return: general function, are stopped at return; as a function of the generator, because of the yield, it will hang when encountered.

I'd like to use one more example to flesh this out. I'm sure you're no stranger to the Fibonacci series, I've mentioned it more than once in previous posts, this time we'll try to apply yield to the Fibonacci series:

def fibs(max):

   """

   fibonacci sequence generator

   """

   n, a, b = 0, 0, 1

   while n < max:

       yield b

       a, b = b, a + b

       n += 1



if __name__ == "__main__":

   f = fibs(9)

   for i in f:

       print(i,end = ' ')

The above code runs as follows:

1 1 2 3 5 8 13 21 34 55

Do you see that the Fibonacci series generated with the generator is different from the previous one? If you are interested, you can compare the implementation of the Fibonacci series I demonstrated in the previous article with the current one, and then take a closer look at the differences.

After these various demonstrations, it's actually pretty clear: a function with a yield statement in it is a generator, i.e., it's also an iterator. This approach is much easier than the previous class to write iterators, but this is not to say that iterators are not good, whether you use iterators or generators to analyze specific problems.

to this article on the Python generator yield detailed article is introduced to this, more related Python yield content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!