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

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全局变量的更多相关文章

  1. python global 全局变量

    http://blog.csdn.net/mldxs/article/details/8559973 __author__ = 'dell' def func(): global x print 'x ...

  2. PYTHON之全局变量

    应该尽量避免使用全局变量.不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性.对全局变量,如果程序员甲修改了_a的值,程序员乙同时也要使用_a,这时可能导致程序中的错误.这种错误是很难 ...

  3. Python的全局变量

    应该尽量避免使用全局变量.不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性.对全局变量,如果程序员甲修改了_a的值,程序员乙同时也要使用_a,这时可能导致程序中的错误.这种错误是很难 ...

  4. Python 的全局变量

    结论: Python 的全局变量只在本文件中生效. 定义全局变量的文件 G.py # define some global variable A = 1 B = 2 C = 3 def fuck(a= ...

  5. Python 中全局变量的实现

    一.概述 Python 中全局变量的使用场景不多,但偶尔也有用武之处. 如在函数中的初始化,有时需要从外部传入一个全局变量加以控制.或者在函数中,使用连接池时,也可能有使用全局变量的需要. 广义上的全 ...

  6. Python实现全局变量的两个解决方法

    Python实现全局变量的两个解决方法 本文针对Python的全局变量实现方法简述如下: 先来看下面一段测试程序:     count = 0 def Fuc(count):   print coun ...

  7. python中全局变量和局部变量的一个小坑

    python 中全局变量和局部变量在使用过程中的一个容易出错的地方 什么是全局变量 python中,在函数外部声明的变量可以叫做全局变量. x = 10 def fn1(): pass fn1() 什 ...

  8. Node入门教程(5)第四章:global 全局变量

    global - 全局变量 全局对象(global object),不要和 全局的对象( global objects )或称标准内置对象混淆.这里说的全局的对象是说在全局作用域里的内的对象.全局作用 ...

  9. PHP:global全局变量的使用

    global全局变量能够让我们更好的去运用,直接上例子: 1.一个函数,获取函数外的内容: 得到的结果: 2.两个函数,函数2获取函数1的全局变量内容:(重点) 结果: 以上就是我的总结啦 END

  10. global 全局变量的用法

    说明:i 和foo()都为全局变量,i 是在模块文件顶层注册的,所以为全局变量,他能够在函数内部进行引用而不需要再特意声明是全局变量,且foo()函数也是全局变量 1.当没有局部变量时,print(i ...

随机推荐

  1. 张超超OC基础回顾02_成员变量(属性),局部变量,全局变量的区别

    成员变量: 写在类声明的大括号中的变量, 我们称之为 成员变量(属性, 实例变量) 成员变量只能通过对象来访问 注意: 成员变量不能离开类, 离开类之后就不是成员变量 成员变量不能在定义的同时进行初始 ...

  2. SpringBoot20 集成SpringSecurity02 -> 利用SpringSecurity进行前后端分离的登录验证

    1 SpirngBoot环境搭建 创建一个SpringBoot项目即可,详情参见三少的相关博文 参考博文 -> 点击前往 SpirngBoot项目脚手架 -> 点击前往 2 引入Spirn ...

  3. suse配置dhcp服务器

    Suse  dhcp服务器安装在安装系统时勾选 Suse dhcp 默认配置文件 /etc/dhcpd.conf Suse dhcp 启动程序 /etc/init.d/dhcpd restart 配置 ...

  4. BT下载的原理 和疑问

    我心中有几个疑问,同时也搜索了点素材,肯能对理解问题有帮助. BT下载,即P2P下载,是一种不需要中心化服务器的下载,实现原理是,每个客户端在下载的时候也作为服务器. 我的疑问是,P2P各个节点是如何 ...

  5. FacadePattern(23种设计模式之一)

    设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...

  6. Windows下用Nginx配置遇到的问题

    Nginx是一款轻量级的web服务器/反向代理服务器,更详细的释义自己百度了.目前国内像新浪.网易等都在使用它.先说下我的服务器软件环境: 系统:Windows Server + IIS + ngin ...

  7. css总结11:css的overflow问题

    1 排版时经常遇到块级元素内容overflow,怎么妥当处理是一个关键. overflow的常用属性:  代码: <!DOCTYPE html><html lang="en ...

  8. 二度xml<一>

    又一次学习Xml,之前差不多都忘了,为了下半年的面试,为了工作重头来过....... 其实我觉得直接上代码来的更实际点,理论的东西,我们随便找点书看看就行. 下面的代码是为了打印出一个xml文件 xm ...

  9. xubuntu14.04LTS安装steam后运行的错误解决办法

    我在ubuntu14.10中没碰到过这个问题,但在xubuntu14.04LTS中碰到 Steam needs to install these additional packages: libgl1 ...

  10. 关于Flag 老是忘掉的东西

    OrderState enums = OrderState.CustomerCanceled | OrderState.CustomerOrdered | OrderState.CustomerQue ...