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

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. WebFlux04 SpringBootWebFlux集成MongoDB之Windows版本、WebFlux实现CRUD、WebFlux实现JPA、参数校验

    1 下载并安装MongoDB 1.1 MongoDB官网 1.2 下载 solutions -> download center 1.3 安装 双击进入安装即可 1.3.1 安装时常见bug01 ...

  2. Installing R under Unix-alikes

    Linux上R的安装 可参考https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_ ...

  3. Asp.net MVC获取访问系统的客户端计算机的主机名和IP地址

    string HostName = string.Empty; string ip = string.Empty; string ipv4 = String.Empty; if (!string.Is ...

  4. 通过网页发布ios应用。

    原文地址:http://www.zhihu.com/question/24304345 两种方法: 1. 测试版本 支持任何类型的开发者帐号,需要在developer后台设置授权deviceID,可以 ...

  5. Python基础-3

    目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...

  6. HTML总结之:HTML5的DOCTYPE 与 meta 属性介绍

    HTML5头部常用介绍: [DOCTYPE html] 声明文档类型为HTML5文件.   [meta标签] <meta> 元素可提供有关页面的元信息(meta-information), ...

  7. 20169219 TCP_IP网络协议攻击实验报告

    (1) ARP缓存欺骗 RP 缓存是 ARP 协议的重要组成部分.ARP 协议运行的目标就是建立 MAC 地址和 IP 地址的映射,然后把这一映射关系保存在 ARP 缓存中,使得不必重复运行 ARP ...

  8. wpf 依赖属性注册解释

    这个解释的很明白了 http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html

  9. Unity5.5.2 CD旋转 顺时针逆时针

    UGUI 下  Sprite_CD  在Inspector下  Image(Script) 下  Clock wise  勾选  决定  CD是顺时针还是逆时针  默认是顺时针  勾选则为逆时针

  10. CentOS 用户/组与权限

    useradd:添加用户 useradd abc,默认添加一个abc组 vipw:查看系统中用户 groupadd:添加组groupadd ccna vigr:查看系统中的组 gpasswd:将用户a ...