zoukankan      html  css  js  c++  java
  • 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()
    
  • 相关阅读:
    旅行计划
    两只塔姆沃斯牛
    迷宫
    异或序列
    异或之和
    素数个数
    SAC E#1
    [JSOI2010]Group 部落划分 Group
    [USACO12FEB]附近的牛Nearby Cows
    [HNOI2008]Cards
  • 原文地址:https://www.cnblogs.com/mephisto03/p/12313302.html
Copyright © 2011-2022 走看看