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. Vue动态组件&异步组件

    在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...

  2. Git 基础教程 之 别名

     配置别名, 例如:       git config --global alias.st status                                      git config ...

  3. windows桌面远程工具连接Ubuntu

    1.Ubuntu安装:sudo apt-get install xrdp    sudo apt-get install vnc4server sudo apt-get install xubuntu ...

  4. 20170704-WNDR4300串口连接信息

    find_hif: bootstrap = 0xaf055aWASP BootROM Ver. 1.1Nand Flash initONFI: Control setting = 0xb44find_ ...

  5. js实现滚动条下拉到一定程度固定结算栏

    实现效果如下: js代码实现如下: var a = $("body").height(); var b = $(window).height(); var c = a - b - ...

  6. App后台开发运维和架构实践学习总结(2)——RESTful API设计技巧

    前言 移动互联网时代,RESTful API成为越来越重要的移动端和服务器端交互的形式.尤其是在很多互联网公司或者传统行业拥抱移动互联网的时候,一套设计良好的Restful API能够帮助互联网产品支 ...

  7. oracle 12c之前用sequence 和 trigger来生成自动增长的列

    SQL> create table scott.t1 (id number, val varchar2(8)); Table created. SQL> CREATE SEQUENCE s ...

  8. hibernate使用c3p0数据源

    在配置好hibernate连接数据库环境的前提下,我们进行例如以下操作就能够搭建好hibernate中使用c3p0数据源的环境了. 1). 导入 jar 包: hibernate-release-4. ...

  9. HDU 4516

    此题不难,但我就是RE,搞不懂啊...郁闷. 说下基本算法吧,只要留意到要分解的因式是(x+ai)..的形式,x前是系数为1的,而且,它们的绝对值在1000以内,于是,好办了.只要枚举(x+k)中的k ...

  10. shell中eval命令

    原文:http://www.cnblogs.com/xdzone/archive/2011/03/15/1984971.html 语法:eval cmdLine eval会对后面的cmdLine进行两 ...