python-global全局变量
在函数内部定义变量时,他们与函数外部具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是局部的,这称为变量的作用域,示例如下:

def func_local(x):
print 'x is', x
x = 2
print 'Chanaged local x to',x x = 50
func_local(x)
print 'x is still', x

执行结果:
x is 50
Chanaged local x to 2
x is still 50
如果想在函数内部改变函数外的变量值,用global语句完成
def func_global():
global y
print 'y is', y
y = 50
print 'Changed local y to', y y = 10
func_global()
print 'Value of y is', y

def func_global():
global y
print 'y is', y
y = 50
print 'Changed local y to', y y = 10
func_global()
print 'Value of y is', y

执行结果:
y is 10
Changed local y to 50
Value of y is 50
y is 10
Changed local y to 50
Value of y is 50
函数参数若是list、set、dict可变参数,在函数内改变参数,会导致该参数发生变化,例如:
def func_local(x):
print 'x is', x
x.append(10)
print 'Chanaged local x to',x x = range(6)
func_local(x)
print 'x is', x

def func_local(x):
print 'x is', x
x.append(10)
print 'Chanaged local x to',x x = range(6)
func_local(x)
print 'x is', x

执行结果
x is [0, 1, 2, 3, 4, 5]
Chanaged local x to [0, 1, 2, 3, 4, 5, 10]
x is [0, 1, 2, 3, 4, 5, 10]
x is [0, 1, 2, 3, 4, 5]
Chanaged local x to [0, 1, 2, 3, 4, 5, 10]
x is [0, 1, 2, 3, 4, 5, 10]
def func_local(x):
print 'x is', x
x.add(10)
print 'Chanaged local x to',x x = set(range(6))
func_local(x)
print 'x is', x

def func_local(x):
print 'x is', x
x.add(10)
print 'Chanaged local x to',x x = set(range(6))
func_local(x)
print 'x is', x

执行结果:
x is set([0, 1, 2, 3, 4, 5])
Chanaged local x to set([0, 1, 2, 3, 4, 5, 10])
x is set([0, 1, 2, 3, 4, 5, 10])
x is set([0, 1, 2, 3, 4, 5])
Chanaged local x to set([0, 1, 2, 3, 4, 5, 10])
x is set([0, 1, 2, 3, 4, 5, 10])
def func_local(x):
print 'x is', x
x['x'] = 2
print 'Chanaged local x to',x x = dict([('x',1), ('y', 2)])
func_local(x)
print 'x is', x

def func_local(x):
print 'x is', x
x['x'] = 2
print 'Chanaged local x to',x x = dict([('x',1), ('y', 2)])
func_local(x)
print 'x is', x

执行结果:
x is {'y': 2, 'x': 1}
Chanaged local x to {'y': 2, 'x': 2}
x is {'y': 2, 'x': 2}
x is {'y': 2, 'x': 1}
Chanaged local x to {'y': 2, 'x': 2}
x is {'y': 2, 'x': 2}
def func_local(x):
print 'x is', x
x = (4, 5, 6)
print 'Chanaged local x to',x x = (1,2,3,)
func_local(x)
print 'x is', x

def func_local(x):
print 'x is', x
x = (4, 5, 6)
print 'Chanaged local x to',x x = (1,2,3,)
func_local(x)
print 'x is', x

执行结果
x is (1, 2, 3)
Chanaged local x to (4, 5, 6)
x is (1, 2, 3)
x is (1, 2, 3)
Chanaged local x to (4, 5, 6)
x is (1, 2, 3)
若传入可变参数如list、set、dict,在函数内部对参数做出修改,参数本身发生变化,tuple、str不变
python-global全局变量的更多相关文章
- python global 全局变量
http://blog.csdn.net/mldxs/article/details/8559973 __author__ = 'dell' def func(): global x print 'x ...
- PYTHON之全局变量
应该尽量避免使用全局变量.不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性.对全局变量,如果程序员甲修改了_a的值,程序员乙同时也要使用_a,这时可能导致程序中的错误.这种错误是很难 ...
- Python的全局变量
应该尽量避免使用全局变量.不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性.对全局变量,如果程序员甲修改了_a的值,程序员乙同时也要使用_a,这时可能导致程序中的错误.这种错误是很难 ...
- Python 的全局变量
结论: Python 的全局变量只在本文件中生效. 定义全局变量的文件 G.py # define some global variable A = 1 B = 2 C = 3 def fuck(a= ...
- Python 中全局变量的实现
一.概述 Python 中全局变量的使用场景不多,但偶尔也有用武之处. 如在函数中的初始化,有时需要从外部传入一个全局变量加以控制.或者在函数中,使用连接池时,也可能有使用全局变量的需要. 广义上的全 ...
- Python实现全局变量的两个解决方法
Python实现全局变量的两个解决方法 本文针对Python的全局变量实现方法简述如下: 先来看下面一段测试程序: count = 0 def Fuc(count): print coun ...
- python中全局变量和局部变量的一个小坑
python 中全局变量和局部变量在使用过程中的一个容易出错的地方 什么是全局变量 python中,在函数外部声明的变量可以叫做全局变量. x = 10 def fn1(): pass fn1() 什 ...
- Node入门教程(5)第四章:global 全局变量
global - 全局变量 全局对象(global object),不要和 全局的对象( global objects )或称标准内置对象混淆.这里说的全局的对象是说在全局作用域里的内的对象.全局作用 ...
- PHP:global全局变量的使用
global全局变量能够让我们更好的去运用,直接上例子: 1.一个函数,获取函数外的内容: 得到的结果: 2.两个函数,函数2获取函数1的全局变量内容:(重点) 结果: 以上就是我的总结啦 END
- global 全局变量的用法
说明:i 和foo()都为全局变量,i 是在模块文件顶层注册的,所以为全局变量,他能够在函数内部进行引用而不需要再特意声明是全局变量,且foo()函数也是全局变量 1.当没有局部变量时,print(i ...
随机推荐
- 824. Goat Latin山羊拉丁文
[抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...
- 面试题:JavaIO流分类详解与常用流用法实例
Java流概念: Java把所有的有序数据都抽象成流模型,简化了输入输出,理解了流模型就理解了Java IO.可以把流想象成水流,里面的水滴有序的朝某一方向流动.水滴就是数据,且代表着最小的数据流动单 ...
- Linux PulseAudio
一.简介 Linux的声音系统或许是最无序的子系统部分!作为Server来说,声音无足轻重,无人问津,而作为桌面来说太多的实现方案,各有各的长出和不足,ALSA经过多年的发展,基本统一了Linux声卡 ...
- 为什么有时候在Windbg中下断点下不了呢??
1.今天我发现我下断点的地方是我声明的变量,变量还仅仅是声明,没有赋值.因此不能下断点. 2.当不能下断点时,如何解决呢? (1)重启Vmware虚拟机.(2)在虚拟机中恢复“快照”.
- 29.MAX() 函数
MAX() 函数 MAX 函数返回一列中的最大值.NULL 值不包括在计算中. SQL MAX() 语法 SELECT MAX(column_name) FROM table_name 注释:MIN ...
- Installing R under Unix-alikes
Linux上R的安装 可参考https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_ ...
- 编写高质量代码改善C#程序的157个建议——建议34:为泛型参数设定约束
建议34:为泛型参数设定约束 “约束”这个词可能会引起歧义,有些人肯能认为对泛型参数设定约束是限制参数的使用,实际情况正好相反.没有“约束”的泛型参数作用很有限,倒是“约束”让泛型参数具有了更多的行为 ...
- LibreOJ 6001 太空飞行计划(最大流)
题解:首先源点向每个实验建边,流量为经费的值,实验向器材建边,值为无限大,器材向终点建边,值为价值 然后跑一遍最大流就能跑出所谓的最大闭合图的点值之和. 代码如下: #include<queue ...
- CentOS7-扩容挂载磁盘
1.1查看新磁盘,可以看到/dev/sdb是新的未挂载的磁盘: [root@localhost ~]# fdisk -l 1.2硬盘分区 ,进入fdisk模式 进入fdisk模式 [root@loca ...
- Stopwatch运行时间 Parallel并行任务
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...