Python yield usage example

Source: Internet
Author: User

1. iterator
The simplest example of the stack generator should be the array subscript. See the following c ++ code:

Copy codeThe Code is as follows:
Int array [10];
For (int I = 0; I <10; I ++)
Printf ("% d", array [I]);

The stacked generator works in a container (array [10]), which extracts values from the container in a certain order (I ++) (array [I]) and perform operations (printf ("% d", array [I]).

The above code is translated into python:

 Copy codeThe Code is as follows:
Array = [I for I in range (10)]
For I in array:
Print I,
 

First, array is a container as a list, and secondly, the list built-in type has the default next action. What is the secret that python has taken after discovering this behavior is not seen by you: take out the array, which is the container's heap generator. next from the inside, give the value to I for processing by the for loop entity. for, print the value.

The problem now is that data can be stacked in containers. Can code be used?

2. constructor

How to change a function into a constructor? You just need yield in the function body!

Copy codeThe Code is as follows:
Def gen ():
Print 'enter'
Yield 1
Print 'Next'
Yield 2
Print 'Next again'

For I in gen ():
Print I

Everybody! In python, yield appears in the gen function, and you can use next. The problem is, how to play next to the Code container?
When I get the iterator from the container, it does not have anything. It is at the container entrance. For arrays, it is the place where the subscript is-1. For functions, it is the function entry, however, if everything is ready, it will be less than next.
Start for I in g, next let itreator crawl to the place where the yield statement exists and return the value,
Next, go to the place where the next yield statement exists and return the values until the function returns (the end of the container ).
The output of the above Code is:
Enter
1
Next
2
Next again

3. Use yield
Yield's code stacking capability not only interrupts function execution, but also records the data at the breakpoint. next time the next book is connected,
This is exactly what recursive functions need.
For example, traverse a binary tree in the middle order:
(It should have been written by David Mertz)

Copy codeThe Code is as follows:
Def inorder (t ):
If t:
For x in inorder (t. left ):
Yield x
Yield t. label
For x in inorder (t. right ):
Yield x
For n in inorder (tree)
Print n

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.