Function call Procedure & Generator explanation

Source: Internet
Author: User

Excerpt from the Marco Solution, thank you. function Call Procedure:

Suppose a program is a single-process, single-execution flow, and at some point there can be only one program flow that can run. But the function call opens the new execution context, so in order to ensure that the main function can be restored to the scene, the main function calls the other functions, you need to first save the main site, put aside, that is, the stack. At this point, the called function executes and, when completed, can be added to the caller main. "Back" caller Main,main can continue to run backwards.

function calls can be nested, for example, X--y--and Z, after Z returns, Y can continue, and until it returns, then X can continue until it ends. In this scenario, X is first in the stack and then y, so Z returns, y out of the stack, y returns, and x out of the stack;

Generator:

When a function that has a yield statement is called, the code inside the function does not execute immediately, but returns only one generator object.

The life cycle of each function starts from being called, to the end of the return, and the next time you enter this function again. But the addition of yield is not so, and each time yield is encountered, it returns a value to the caller, and then the current function pauses (rather than exits), and then the control is returned to the caller.

Yield is the equivalent of return, except that the return ends the current function, but the yield is paused, which saves the scene.

So the next time this function is called, it continues from the last saved state, not from the beginning.

Instance:
1 #!/usr/bin/env python2 #Date:2016/02/273 #4 5 deffn (x):6RET = 17      forIinchRange (1, x+1):8RET *=I9         yieldretTen  One  forXinchFN (5): A     Print(x) -     Print('-'*20)

Function call Procedure & Generator explanation

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.