2018.12.14——函数作用域

一、引入:

例1:

def test1():
    print('in the test1')
def test():
    print('in the test')
    return test1
res=test()
print(res)

输出结果:

例2:

def test1():
    print('in the test1')
def test():
    print('in the test')
    return test1
res=test()
print(res())

输出结果:

 例3:

name='alex'
def foo():
    name='linhaifeng'
    def bar():
        print(name)
    return bar
foo()

#输出结果:

对比一下(做了一些微调):

name='alex'
def foo():
    name='linhaifeng'
    def bar():
        print(name)
    return bar
print(foo())

#输出结果:bar的内存地址

猜你喜欢

转载自www.cnblogs.com/lijialun/p/10117728.html