python中LEGB原则

 

1、

>>> def a():
    x = 200
    def b():
        print("inner:",x)
    return b()

>>> a()
inner: 200

 

2、

>>> def a():
    x =  100
    def b():
        x = 300
        print("inner:",x)
    return b()

>>> a()
inner: 300

 

3、

>>> x = 300
>>> def a():
    def b():
        print("inner:",x)
    return b()

>>> a()
inner: 300

 

4、

>>> x = 300
>>> def a():
    x = 500
    def b():
        print("inner:",x)
    return b()

>>> a()
inner: 500

 

5、

>>> x = 300
>>> def a():
    x = 500
    def b():
        x = 700
        print("inner:",x)
    return b()

>>> a()
inner: 700

 

posted @ 2021-03-06 16:55  小鲨鱼2018  阅读(132)  评论(0编辑  收藏  举报