Scope of functions (running of nested functions)

def test1():
    print("hello world")
def test():
    print("in the tese")
    return test1

res = test() #res = test(1) #No return default returnnone No print print is to get the memory #The       scope of the function is only related to the scope defined when the function is declared, and has nothing to do with the calling location of the function
print(res())

The scope is to ignore global variables and local variables, and only run the corresponding function.

 

name='alex'

def foo():
    name='lhf'
    def bar():
        name = ' wupeiqi '    #Function nesting is layer by layer nesting, layer by layer, except that the innermost layer is one layer less.
         print (name)
         def tt():
             print (name)
         return tt
     return bar

# bar=foo() 
# tt=bar() 
# print(tt) 
# tt() 

foo()()() # run tt() two brackets run bar() run first crate (first bracket ) to get the second crate, this code block has four crates

 

Guess you like

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