Python yield usage

Source: Internet
Author: User

Yield is simply a generator. The generator is a function that remembers the position in the function body during the last return. The second (or NTH) Call to the generator function jumps to the center of the function, while all local variables in the last call remain unchanged. The generator is a function. All parameters of a function are retained. The parameters used when the function is called for the second time are retained for the previous time. the generator also "remembers" it not only "remembers" its data status in the throttling constructor. The generator also "remembers" its position in the flow control Constructor (in imperative programming, this constructor is not just a data value. Because continuity allows you to jump between execution frameworks without always returning the context of the direct caller (as in the builder), it is still relatively general. Yield generator running mechanism when you ask the generator for a number, the generator will execute until the yield statement appears. The generator will give you the yield parameter, and then the generator will not continue to run. When you ask him to ask for the next number, it will run from the last status until the yield statement appears, give the parameter to you, and then stop. Until the function is exited. Here are a few examples to understand yield: Example 1 yield application: def addlist (alist): for I in alist: yield I + 2 alist = [1, 2, 3, 4] for x in addlist (alist): # print x for each list data in alist. output result: 3456 Example 2. next's application: def h (): print 'hello' yield 5 print 'word' c = h () # execute to yield c. next () print 'flag' # program error c. next () output result: helloflagword www.2cto. comTraceback (most recent call last): File "test. py ", line 9, in <module> c. next () www.2cto. ComStopIteration example 3. difference between next and send # coding: utf8def h (): print 'hello' m = yield 5 print m d = yield 12 print 'word' c = h () # c. next () is equivalent to c. send (None) c. next () print 'flag' # (yield 5) is assigned, and yield receives the send value c. send ('! ') Example 4. returned values of next and send # coding: utf8def h (): print 'hello' m = yield 5 print m d = yield 12 print 'word' m = c. next () print 'flag' d = c. send ('! ') # M, the value of d is 5, 12. The returned values of send and next are the print m, d parameters of yield.

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.