欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python学习-26 函数作用域

程序员文章站 2022-07-09 21:03:03
举例说明: 1. 运行结果: 2. 运行结果: ......

 举例说明:

 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