Python----Special closures

Source: Internet
Author: User

1, the definition of closures:

When a child function has a call to a parent function variable (not a global variable) and returns a child function name, the parent function does not end the release as the function runs, but instead saves the state waiting for the call of the child function.

Our common types of closures:

def fun1 ():     = 1    def  fun2 ():        return  x    return fun2
g = fun1 ()
Print (g ())

Such closures are sure to be visible at a glance.

So, what about this:

#1
deffun1 (): Lis=[] Name='Alex' deffun2 ():Print(name) lis.append (fun2)returnLISG=fun1 () g[0] () #2lis= []deffun1 (): Name='Alex' deffun2 ():Print(name) lis.append (fun2) G=fun1 () lis[0] ()

Run the above program you will find that it is actually closures, the first fun2 called the parent function's name variable, and then the function name is returned by putting it in the list. In the second, the function name is returned through a mutable list of global variables, so it is also a closure function.

Python----Special closures

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.