Python's Way--day10-closure function

Source: Internet
Author: User
Tags closure

1. Named keyword parameters

Format: Parameters after * are named keyword parameters


1. Must be transmitted value

2. The caller of the constraint function must pass the value in the form of Key=value

3. The caller of the constraint function must use the key name we specified

1 defAuth (*args,**Kwargs):2     """3 mode of Use Auth (name= "Egon", pwd= "123")4 :p Aram args:5 :p Aram Kwargs:6 : return:7     """8     #print (Args,kwargs)9     ifLen (args)! =0:Ten         Print('must be passed in the form of a keyword') One         return A     if 'name'  not inchKwargs: -         Print('must use the specified key name name') -         return the     if 'pwd'  not inchKwargs: -         Print('must use the specified key name pwd') -         return -  +name=kwargs['name'] -pwd=kwargs['pwd'] +     Print(NAME,PWD)

2. Function nesting

1. Nested invocation of a function: Another function is called within a function


def Max (x, y    ): if x>y:        reryrn x    Else:        return  ydef      max3 (x, Y, z): = max2 (x, y)    = max2 (rea1,z)    return Res2 Print (Max3 (11,199,2))

2, nested definition of function: Other functions defined inside the function

def func1 ():     Print ('fromfunc1')     def # func2= memory address         Print ('fromfunc2') func1 ()

defF1 ():Print('F1')    defF2 ():Print('F2')        deff3 ():Print('f3') F3 () F2 () F1 ( )" "f1f2f3" "

3. Namespaces and Scopes

1. Namespaces: Where names and value bindings are stored

1.2. Classification of namespaces:

1.2.1 Built-in namespaces: holds the Python interpreter's own name, takes effect when the interpreter starts, and fails when the interpreter is closed

1.2.2 Global Namespace: A file-level name that takes effect when the file is executed and expires at the end of the file or when the file is deleted

1.2.3 Local namespaces: Names defined within the store function (parameters of functions and names within functions are stored in local variable spaces)---are temporarily in effect when function calls and end of function are invalidated

Load order: Built-in namespaces-----Global namespaces---local namespaces

Find Names: Local namespaces-----Global namespaces-----built-in namespaces

2: Scope

1. Global variable field: Contains the name of the built-in namespace and the global namespace


can be accessed in any location

Names within that range will accompany the program's entire life cycle.

2. Local scope: Contains the name of the local namespace


Can only be used within a function

Call function is in effect, end of call is invalidated

4, Function object

1, functions can be referenced

def Bar ():     Print(' frombar'=barf ()

2, can be passed as a parameter


def Bar ():         print(' from Bar ')def  Wrapper (func):        func () wrapper (bar) 

3, can be used as the return value of the function

def Bar ():     print(' from Bar ')def  foo (func):    return= foo (bar) f ()

4, can be used as an element of the container type

def get ():      Print ('fromget') def put ():      Print ('fromput') L=[get,put]print(l) l[0] () 
defauth ():Print('Log in .....  ')defreigster ():Print('register ..... ')defsearch ():Print('view .... ')deftransfer ():Print('transfer .... ')defPay ():Print('pay .... ') DiC={    '1': Auth,'2': Reigster,'3': Search,'4': Transfer,'5':p ay}defInteractive (): whileTrue:Print("""1 Certification 2 Registration 3 View 4 Transfer 5 Payment""") Choice=input ('>>:'). Strip ()ifChoiceinchDic:dic[choice] ()Else:            Print('Illegal Operation') Interactive ()

5, Closure function

A function defined inside a function that contains a reference to the name in the scope of the outer function, which becomes the closure function

def outter ():     name='Egon'     def inner ():          Print ('My name is%s' %name)      return Inner

Inner () is called a closure function.
Inner () defined outside the function outter, the scope internal variable of the Outter function name, referenced within the inner

Python's Way--day10-closure function

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: 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.