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 ...
随机推荐
- php常用 随机数
<?php $number =range(1,50); //shuffle 将数组顺序随即打乱. shuffle($number); print_r($number); echo '<br ...
- 使用clr 调用C#编写的dll中的方法的全解释
使用clr 调用C#编写的dll中的方法的全解释1.数据库初始化:将下面这段代码直接在运行就可以初始化数据库了exec sp_configure 'show advanced options', '1 ...
- Luogu 2704 [NOI2001]炮兵阵地
唔,想到了状压之后就不会了……实在是菜 考虑压两行,设$f_{i, j, k}$表示当前到第$i$行,上一行是$j$状态,前一行是$k$状态的最多能放的炮兵的数量. 发现第一维还可以滚掉,好像可以转移 ...
- ubuntu安装软件包apt-get和dpkg方法
1apt方法 安装软件 apt-get install softname1 softname2 softname3…… 卸载软件 apt-get remove softname1 softname2 ...
- HttpUploader6.2-process版本
1.优化JS逻辑,在上传前先同步相同文件进度,提高多用户上传效率. 2.优化文件块保存逻辑,减少相同文件块的写入操作,减少服务器IO操作,提高上传效率. js变化: up6.js新增UrlQuer ...
- (转)通过Javascript得到URL中的参数(query string)
原文地址:http://www.cnblogs.com/season-huang/p/3322561.html 我们知道,"GET"请求中,通常把参数放在URL后面,比如这样htt ...
- C# static 字段初始值设定项无法引用非静态字段、方法或属性
问题:字段或属性的问题字段初始值设定项无法引用非静态字段.方法 下面代码出错的原因,在类中定义的字段为什么不能用? public string text = test(); //提示 字段或属性的问题 ...
- .netcore部署到IIS上出现HTTP Error 502.5 - Process Failure问题解决
首先网上是有很多解决方案,但是对我这个错误完全没用 如果你们没有环境首先得预装环境如下 1.首先在bing.com下搜索asp.net core download, 然后打开搜索到的信息.NET Do ...
- C#构造函数用法
1.实例构造函数 2.静态构造函数 3.私有构造函数 例: 创建一个类 using System; using System.Collections.Generic; using System.Lin ...
- WinForm中DataGridView的使用(五) - 自定义列
DataGridView支持指定DataGridViewImageColumn.DataGridViewButtonColumn等特殊类型的列,加入到Columns中. DataGridViewIma ...