python基础-作用域

函数的作用域在函数定义的时候就已经固定死了,与调用它的位置无关。

name='lhf'
def test():
    name='alex'
    def test_1():
        print(name)
    return test_1
test()()

  

def test():
    print("from test")
def test_1():
    print('from test_1')
    return test
res=test_1()#返回的是内存地址。return返回值可以是任意类型
print(res)
def test():
    print("from test")
    def test_1():
        print('from test_1')
        def test_1_1():
            print('from test_1_1')
        return test_1_1
    return test_1
test()()()#每次返回的都是子函数的内存地址,加上()就是运行。注意:调用子函数必须调用母函数。虽然在外部调用test——1——1,但打印的还是from test——1——1

posted @ 2020-11-24 00:07  枫叶学python  阅读(100)  评论(0)    收藏  举报