代码的顺序也是需要非常精确的调用时机的

x=20
def outer():
    y = 99
    def inner():
        nonlocal y
        y = 9658
        print('inner:',x,y)
    inner() #point1
    print('outer',x,y)
    #inner() #point2
print('golbal:',x)
outer()

若是在point1处执行inner(),后执行print语句,结果:

golbal: 20
inner: 20 9658
outer 20 9658

若是在point2处执行inner(),先执行print语句,结果:

golbal: 20
outer 20 99

inner: 20 9658
posted @ 2026-10-13 07:17  叕叒双又  阅读(3)  评论(0)    收藏  举报