Python之global
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的更多相关文章
- Python的global指令的作用
Python的global指令的作用 学过其他常用编程语言的同学一定清楚,Python是一个语法非常宽容的语言.它是个变量的定义可以省略.直接赋值.根据赋值结果自动确定变量类型的弱类型语言. 但是这样 ...
- python中global 和 nonlocal 的作用域
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 . 一 global global关键字用来在函数或其他局部作用域 ...
- 【python】global
#!/usr/bin/python # Filename: func_global.py def func(): global x print 'x is', x x = 2 print 'Chang ...
- python中global和nonlocal用法的详细说明
一.global 1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字. gcount = 0 def global_test(): ...
- python中global 和 nonlocal的使用
1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字. gcount = 0 def global_test(): gcount+=1 ...
- python GIL(Global Interpreter Lock)
一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...
- python中global变量释疑
疑问 为什么main中不能写global x语句? 在函数中如何修改global变量? 在main中修改了global变量后,在子进程中为什么没有效果? 如何利用进程池的initializer参数(函 ...
- Python 中 global、nonlocal的使用
1.在学习python过程中很重要的一点需要记住:如果在函数内部定义了跟全局变量同名的变量,那么该变量将是局部变量,而全局变量的引用在该函数范围内将失效. x = 9 def a(): x = 10 ...
- python中global的用法——再读python简明教程
今天看了知乎@萧井陌的编程入门指南,想重温一下 <python简明教程>,对global的用法一直不太熟练,在此熟练一下,并实践一下python中list.tuple.set作为参数的区别 ...
随机推荐
- 基于vue的nuxt框架cnode社区服务端渲染
nuxt-cnode 基于vue的nuxt框架仿的cnode社区服务端渲染,主要是为了seo优化以及首屏加载速度 线上地址 http://nuxt-cnode.foreversnsd.cngithub ...
- Python 输出带颜色的文字方法
输出文字带颜色 书写格式,和相关说明如下: #格式: 设置颜色: \033[显示方式;前景色;背景色m \033[0m 方法: 字体色 背景色 颜色 -------------------- ...
- BZOJ 1230 Usaco2008 Nov 开关灯
[题意概述] 给出一个01序列,初始时序列全为0,每次有修改操作或询问操作,修改操作要求把L~R区间中的0变成1,1变成0,查询操作要求输出L~R区间的1的个数 [题解] 线段树. 每次区间修改把区间 ...
- 00105_UDP和TCP协议
1.UDP协议 (1)UDP是User Datagram Protocol的简称,称为用户数据报协议: (2)UDP是无连接通信协议,即在数据传输时,数据的发送端和接收端不建立逻辑连接: (3)当一台 ...
- [POJ2912]Rochambeau(并查集)
传送门 题意: n个人分成三组,玩石头剪子布游戏,同一组的人只能出同样固定的的手势,其中有一个是裁判不属于任何组,可以出任意手势,给出m个信息x op y 表示x,y是从三个组里面随机抽取的或者是裁判 ...
- 1066N !最右边非零数
http://hi.baidu.com/nicker2010/item/4fa83c4c5050b3e5a4c066ec 另一个 Last non-zero Digit in N! Time Limi ...
- Debug 集子
一. 001.c: 在函数 'main' 中: 001.c:8: 错误:'start' 的存储大小未知 001.c:9: 错误:'end' 的存储大小未知 ====================== ...
- React Native Mac配置指南
步骤 http://facebook.github.io/react-native/docs/getting-started.html#content 依照React Native官网步骤一步步安装, ...
- Unable to instantiate Action, xxxAction, defined for 'xxx' in namespace '/'xxxAction解决方式
出现这个问题的解决办法主要有两个 1.假设项目没有使用Spring,则struts.xml配置文件里,这个action的class属性的路径没有写完整,应该是包名.类名 2.假设项目使用了Spring ...
- iOS7系统iLEX RAT冬青鼠安装教程:无需刷机还原纯净越狱系统
全网科技 温馨提醒:iLEX RAT和Semi-Restore的作用都是让你的已越狱的设备恢复至越狱的初始状态. 可是要注意无论你是用iLexRAT冬青鼠还是Semi-restore.对于还原来说都存 ...