Python中的作用域

Python中的作用域分4种:

  • L : local ,局部作用域
  • E : enclosing,嵌套的父级函数的局部作用域, 即包含此函数的上级函数局部作用域, 但不是全局的
  • G : globa, 全局变量, 就是模块级别定义的变量
  • B : built-in, 系统固定模块里边的变量,比如int, byte,array等

搜索变量的优先级顺序依次是: 作用域局部 > 外层作用域 > 当前模块中的全局 > python内置作用域
即 LEGB

x = int(2.9) # int built-in
g_count = 0 # global
def outer():
    o_count = 1 # enclosing
    def inner():
        i_count = 2 #local
        print(i_count)
    print(o_count)
    #print(i_count) # 找不到
#inner()
t = outer()
posted @ 2020-02-15 19:39  无聊的子木君  阅读(147)  评论(0编辑  收藏  举报