Function Nesting and Closures Try Decorators

What is function nesting:

Define a function inside the function

def foo():
    print('from foo')
    def test():
        pass
def father(auth_type):
     print('from father %s' %name) 
    def son():       #Calling the son function can only be called locally in this row and cannot be called outside, such as a son below the father ()
  print (locals()) #locals is a local variable. There are two local variables at this level. One is the parameter 'filedb' and the address of the function son (according to rheumatism theory, the function is a variable, so the son function is also a variable)

father( ' filedb ' )

Closures (actually similar to scopes but another way of saying it): A package is a package of nested functions. How to use: If there is no name in the second and third layers, then give a name value in the outermost layer, and the innermost print will print the name, which penetrates layer by layer.

 

#Infiltration demo 

def
father(name): # print("My father is %s" %name) def son(): # name = "Long Kuiqi" # print("My son is %s" %name) def grandson(): # name = "wuyanna" print ( " My son is %s " % name) grandson() son() father( " Long Kuiqi " )

 

 

A closure is a closed package that contains encapsulated variables. 

For example, the innermost package, grandson: it encapsulates a name. Then the next package son: only one grandson function is encapsulated in it. The outermost layer encapsulates the son function and the passed name (variable).

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987244&siteId=291194637