Python老男孩 day16 函数(五) 函数的作用域

https://www.cnblogs.com/linhaifeng/articles/6113086.html

——————————————————————————————————————

八、函数的作用域

 

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

res=test()   #res接收到test1
print(res)   #相当于print(test1)
print(res()) #res()相当于运行test1()

 

运行结果:
in the test
<function test1 at 0x0000021BA1C01EA0> #输出test1的内存地址
in the test1
None #test1没有返回值,所以print输出None

 

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

运行结果:
<function foo.<locals>.bar at 0x0000020F90680C80> #bar的内存地址
wupeiqi

 

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

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

运行结果:
<function foo.<locals>.bar at 0x0000012D3D170C80>
linhaifeng

 

name='alex'

def foo():
    name='lhf'
    def bar():
        name='wupeiqi'
        def tt():
            print(name)
        return tt
    return bar

func=foo()   #func=bar
func()()     #func()=bar()=tt      func()()=tt()

运行结果:
wupeiqi

 

posted @ 2018-05-30 14:25  小飞侠Kobe  阅读(145)  评论(0编辑  收藏  举报