Python Day4 函数编程与内置函数

#作用域
def test1():
    print(2)
def test():
    print(1)
    return test1
res=test()#运行test,返回结果赋值给res
print(res())#相当于运行test1

name='lzx'
def foo():
    name='lcf'
    def bar():
        print(name)
    return bar
fun=foo()#把bar的地址赋值给fun,fun()即可运行bar()
print(fun)
fun()#执行bar,得到的结果是lcf而不是lzx
#函数作用域只与定义位置有关,和调用位置没有关系

猜你喜欢

转载自www.cnblogs.com/lzxanthony/p/9283808.html