python学习-26 函数作用域

 举例说明:

 1.

name = 'john'
def foo():
    name = 'xiaomming'
    def bar():
        print(name)
    return  bar


a=foo()
print(a)
a()

运行结果:

<function foo.<locals>.bar at 0x0382A6A8>        # 内存地址
xiaomming

Process finished with exit code 0

 

2.

def foo():
    name = 'xm'
    def bar():                                        ‘’‘
        name = 'xh'
        def abc(): 
            print(name)                                作用域
        return abc
    return bar                                         ’‘’
foo()()()

运行结果:

xh

Process finished with exit code 0

 

posted @ 2019-07-04 14:56  学python的菜鸟  阅读(162)  评论(0编辑  收藏  举报