函数的作用域(嵌套函数的运行)

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

res = test()  #res = test(1) #没有return默认returnnone 没有打印print就是得到内存地      #函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
print(res())

作用域即忽视全局变量与局部变量,只运行对应的函数。

name='alex'

def foo():
    name='lhf'
    def bar():
        name='wupeiqi'   #函数嵌套是一层层嵌套,层层递进,除了最里面那一层少一层不都不行
        print(name)
        def tt():
            print(name)
        return tt
    return bar

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

foo()()() #运行tt() 两个括号运行bar() 运行第一个箱子(第一个括号)得到第二个箱子,这个代码块有四个箱子

猜你喜欢

转载自www.cnblogs.com/newt/p/8997273.html
今日推荐