zoukankan      html  css  js  c++  java
  • Python3 函数作用域

     1 # if True:
     2 #     x = 3
     3 #
     4 # print(x)
     5 #
     6 # def test():
     7 #     a = 10
     8 # print(a)    # a未定义
     9 
    10 '''
    11 Python中的作用域分为四种情况:
    12 1.L:local,局部作用域,在函数中定义的变量
    13 2.E:enclosing,嵌套的父级函数的局部作用域,包含此函数的上级函数的局部作用域,但不是全局的
    14 3.G:global,全局变量,模块级别定义的变量
    15 4.B:built-in,系统固定模块里面的变量,如int,bytearray等。
    16 
    17 搜寻变量优先级:L>E>G>B
    18 '''
    19 # 顺序例子
    20 # x = int(5.8)    # built-in
    21 # a = 1   # global
    22 # def test1():
    23 #     b = 2   # enclosing
    24 #
    25 #     def test2():
    26 #         c = 3   #local
    27 #         print(c)
    28 #         print(b)
    29 # #    print(c)    # 未定义
    30 #     test2()
    31 # test1()
    32 
    33 # count = 10
    34 # def outer():
    35 #     print(count)
    36 # #    count +=1  # 不能修改全局变量,除非创建变量
    37 # outer()
  • 相关阅读:
    printf()参数的处理
    const关键字作用
    static的作用
    程序调试的方法
    引用与指针的区别
    内存对齐
    Android UI开发详解之ActionBar
    Android ActionBar使用方法
    android的系统学习
    Activity 切换动画
  • 原文地址:https://www.cnblogs.com/Infi-chu/p/7666112.html
Copyright © 2011-2022 走看看