Use of the yield Expression of the generator function in Python 3, pythonyield

Source: Internet
Author: User

Use of the yield Expression of the generator function in Python 3, pythonyield

 

The generator function or generator method contains a yield expression. When a generator function is called, an iterator is returned, and the value is extracted from the iterator each time (by calling its _ next _ () method ). Every time _ next _ () is called, the value of the yield Expression of the generator function (if not specified, it is None) will be returned. If the generator function ends or executes a return, a StopIteration exception occurs.

The above statement is somewhat official. Below are some personal understandings:

1. When a function contains a yield expression, when calling this function, the function code does not run, but returns an iterator, or a container that can be used for iteration (equivalent to the number of sequences in an array ). In the following example, the code is not run when a () is called.

>>> def a():            print('a')            yield 'x'>>> a()<generator object a at 0x0000027BBFB380F8>>>>     

 

2. To run this function, an iterator is required to read the content in the container. for example, the for statement is as follows:

>>> for i in a():             ia'x'>>>     

 

 

3. a yield expression will only generate "one" iterator. Then, let's explain that this iterator is equivalent to the sequence ['A', 'B ', the positions of 'A', 'B', and 'C' in 'C'] are 0, 1, and 2. If there are multiple iterations, when the iterator is called, there will be multiple iterations:

def b():    print('a')    yield 1    print('b')    yield 3for i in b():    print(i)a1b3>>> 

 

In this example, the for loop is executed twice. The first iteration is the iterator generated by yield 1 (it can also be simply understood as the first serial number 0 of the sequence ), returns the print ('A') result of result 1 and the front edge of the yield expression. The second iteration is the iterator generated by the yield 3 expression, the result is the result of the yield 3 expression's return value 3 and the print ('B') statement on its front side.

4. yield also has a return function to some extent-A value ('x' of the above function) will be returned, but yield will not terminate the function, but suspend the function, A StopIteration exception is generated until all iterations are used to terminate the function.

5. Use the iter function to view the specific process of the iterated structure.

You can use the = iter (iterable structure) to obtain an iterator. In each loop, you can use the next (a) method to obtain the next data item. a StopIteration exception occurs at the end.

Function B () is equivalent to a container that contains two iterations;

>>> c=iter(b())>>> next(c)a1>>> next(c)b3>>> next(c)Traceback (most recent call last):File "<pyshell#6>", line 1, in <module>next(c)StopIteration>>> 

 

The first and second iterations return the results of the two iterations respectively. When all iterations are used, the third iteration produces a StopIteration.

6. yield returns the iteration subvalue. yield can set any content as the iteration subvalue. In most cases, the iterations we understand are 0, 1, 2, 3, 4... this is a natural position iteration, and yield sets it to any value. The iterations generated by function a () are 'x', And the iterations generated by function B () are 1 and 3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.