1  Global

  The global statement and its nonlocal cousin are the only things that are remotely like declaration statements in Python. They are not type or size declarations; they are namespace declarations. The global statement tells Python that a function plans to change one or more global names.

  • Global names are variables assigned at the top level of the enclosing module file.
  • Global names must be declared only if they are assigned within a function.
  • Global names may be referenced within a function without being declared.

  In other words, global allows us to change names that live outside a def at the top level of a module file.

2  Example

  The global statement consists of the keyword global, followed by one or more names separated by commas.

X = 88                         # Global X

def func():
global X
X = 99 # Global X: outside def func()
print(X) # Prints 99

  

y, z = 1, 2                    # Global variables in module

def all_global():
global x # Declare globals assigned
x = y + z # No need to declare y, z: LEGB rule

  x, y, and z are all globals inside the function all_global. y and z are global because they aren’t assigned in the function; x is global because it was listed in a global statement to map it to the module’s scope explicitly. Without the global here, x would be considered local by virtue of the assignment.

3  Access globals

# thismod.py
var = 99 # Global variable == module attribute def local():
var = 0 # Change local var def glob1():
global var # Declare global (normal)
var += 1 # Change global var def glob2():
var = 0 # Change local var
import thismod # Import myself
thismod.var += 1 # Change global var def glob3():
var = 0 # Change local var
import sys # Import system table
glob = sys.modules['thismod'] # Get module object (or use __name__)
glob.var += 1 # Change global var def test():
print(var)
local();
print(var)
glob1();
print(var)
glob2();
print(var)
glob3()
print(var)

  run and get results

>>> import thismod
>>> thismod.test()
99
99
100
101
102

Python之global的更多相关文章

  1. Python的global指令的作用

    Python的global指令的作用 学过其他常用编程语言的同学一定清楚,Python是一个语法非常宽容的语言.它是个变量的定义可以省略.直接赋值.根据赋值结果自动确定变量类型的弱类型语言. 但是这样 ...

  2. python中global 和 nonlocal 的作用域

    python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 . 一 global global关键字用来在函数或其他局部作用域 ...

  3. 【python】global

    #!/usr/bin/python # Filename: func_global.py def func(): global x print 'x is', x x = 2 print 'Chang ...

  4. python中global和nonlocal用法的详细说明

    一.global 1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字.   gcount = 0 def global_test(): ...

  5. python中global 和 nonlocal的使用

    1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字. gcount = 0 def global_test(): gcount+=1 ...

  6. python GIL(Global Interpreter Lock)

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  7. python中global变量释疑

    疑问 为什么main中不能写global x语句? 在函数中如何修改global变量? 在main中修改了global变量后,在子进程中为什么没有效果? 如何利用进程池的initializer参数(函 ...

  8. Python 中 global、nonlocal的使用

    1.在学习python过程中很重要的一点需要记住:如果在函数内部定义了跟全局变量同名的变量,那么该变量将是局部变量,而全局变量的引用在该函数范围内将失效. x = 9 def a(): x = 10 ...

  9. python中global的用法——再读python简明教程

    今天看了知乎@萧井陌的编程入门指南,想重温一下 <python简明教程>,对global的用法一直不太熟练,在此熟练一下,并实践一下python中list.tuple.set作为参数的区别 ...

随机推荐

  1. 使用vscode,新建.vue文件,tab自动生成vue代码模板

    第一步: 新建模板并保存 文件 --> 首选项 --> 用户代码片段 --> 输入vue,选择vue.json -->复制 第三步中的模板内容中内容保存 第二步: 添加配置,让 ...

  2. 获取当前日期,或指定日期的农历js代码

    时间不早了,直接上代码啦-- var CalendarData=new Array(100);var madd=new Array(12);var tgString="甲乙丙丁戊己庚辛壬癸& ...

  3. Spring学习总结(4)——Spring AOP教程

    一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...

  4. 常用Git命令大全思维导图

    开发中代码管理少不了使用Git,对于初学者来说Git命令的学习是一个难过的坎,为了帮助大家记忆并快速掌握Git的基本使用,我把常用的Git命令整理成思维导图,分享给大家. 高清大图在线预览 http: ...

  5. 洛谷 P2827 BZOJ 4721 UOJ #264 蚯蚓

    题目描述 本题中,我们将用符号表示对c向下取整,例如:. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现在共有n只蚯蚓(n为正整数).每只 ...

  6. java 垃圾收集

    1.为什么使用垃圾收集 a.把用户从释放占用内存的重担中解救出来 b.帮助程序保持完整性 2.垃圾收集算法 检测出垃圾对象,必须回收垃圾对象所使用的堆空间并还给程序 垃圾检测:通过建立一个根对象集合并 ...

  7. 子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  8. 安卓实训第九天---Activity的复习以及在Onstart里设置网络连接

    今天.首先对Activity的生命周期进行复习: (以下的截图部分是借鉴自赵雅智老师的博客.. .) Activity的完整生命周期自第一次调用onCreate()開始.直至调用onDestroy() ...

  9. jenkins下载插件失败解决办法

  10. 关于strace的一点东西

    好久没写博客了,感觉有点羞愧,认为自己也应该静下心来利用自己可分配的时间去提升自己.        尽管近期在看一些Python的东西,但是认为自己还是不能忘记本行啊,Linux C的一些东西必须一直 ...