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

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. 基于rank的优化

    ------------------siwuxie095                                 基于 rank 的优化         基于 size 的优化,在大多数情况下 ...

  2. ssh试卷

    2.简述Hibernate的工作原理. 答:首先,Configuration读取Hibernate的配置文件及映射文件中的信息,即加载配置文件和映射文件,并通过Hibernate配置文件生成一个多线程 ...

  3. [GO]随机数的使用

    package main import ( "math/rand" "time" "fmt" ) func main() { //设置种子, ...

  4. css总结12:CSS Float(浮动)

    1 定义:CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. 2 解释:浮动的元素,浮在界面上,脱离了HTML文本流. 元素的水平方向浮动,意味着元素只能左右移动而不能 ...

  5. libtool: syntax error near unexpected token `]*

    ../libtool: line 543: syntax error near unexpected token `]*' ../libtool: line 543: `      *[\[\~\#\ ...

  6. 软工作业WordCount

    github项目传送门:https://github.com/zzh010/My-wc 一.WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命 ...

  7. Xcode更新至IOS 9 后错误处理

    1.obtain an updated library from the vendor, or disable bitcode for this target. for architecture ar ...

  8. kubernetes 1.3管中窥豹- RS(Replica Sets):the next-generation Replication Controller

    前言 kubernates 1.3出了几个新的概念,其中包括deployments,Replica Sets,并且官网称之为是the next-generation Replication Contr ...

  9. 《C#多线程编程实战》2.5 AutoResetEvent

    这个有点像是缩小版的mutex 还是很好理解的 相对mutex 使用的范围可能比较小一点. class Program { static AutoResetEvent work = new AutoR ...

  10. 【转】C# String 与 Char[] 数组 相互转换

    源地址:http://blog.csdn.net/razilfelix/article/details/52289663 string 转换成 Char[] string ss = "abc ...