python_day016 scoping

作用域练习1

def test1():
    print('in the test1')
def test():
    print('in the test')
    return test1
res = test()
print(res()) #res = test1地址 函数没有return,默认返回None

作用域练习2

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

作用域练习3

name = 'alex'
def foo():
    name = 'lhf'
    def bar():
        name = 'wupeiqi'
        print(name)
        def tt():
            print(name)
        return tt
    return bar
bar = foo()
tt = bar() 
print(tt)
print(tt())
#上面几句=print(foo()()())

猜你喜欢

转载自www.cnblogs.com/yuyukun/p/10397854.html
今日推荐