Python yield Use Method Example _python

Source: Internet
Author: User

1. Iterator
The simplest example of a iterator should be an array subscript, and look at the following C + + code:

Copy Code code as follows:

int array[10];
for (int i = 0; i < i++)
printf ("%d", array[i]);

The iterator works in a container (array[10]), and it extracts the value (Array[i] from the container in a certain order (i++) and operates (printf ("%d", array[i)).

The code above is translated into Python:

Copy Code code as follows:

Array = [I for I in range (10)]
For I in array:
Print I,

First of all, array as a list is a container, followed by the list of the built-in type has the default next behavior, Python found these after the secret of the action is not seen by you: Take out the array of the YA container of the iterator, From the inside, next. Give me the value to I for the loop body disposal, for the value print.

Now the problem is that the data can be used to do container iterations, can the code?

2. Constructor

How to turn a function into a constructor? There is a yield in the function body!

Copy Code code as follows:

Def gen ():
print ' Enter '
Yield 1
print ' Next '
Yield 2
print ' Next again '

For I in Gen ():
Print I

You! Python sees the yield in the Gen function and knows you can use next, and the question is, how do you play next to the code container?
When you get the iterator from the container, it is nothing, at the entrance of the container, for the array is subscript-1 place, for the function is the function of the entry is not dry, but everything is ready to owe next.
Start for I in G,next let Itreator crawl to the place where the yield statement exists and return the value,
Next again, crawl to the place where the next yield statement exists and return the value, in turn until the function returns (at the end of the container).
You must have seen the output of the above code is:
Enter
1
Next
2
Next again

3. Use of yield
Yield's code iteration capability can not only interrupt function execution but also write down the data at the breakpoint, next time,
This is exactly what a recursive function needs.
For example, the sequence traversal binary tree:
(It should have been written by David Mertz)

Copy Code code 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.