Python Variable Scope
Python中的变量的作用域有时会让像我这样的初学者很头疼。
其实只需要掌握以下两点:
1. Python能够改变变量作用域的代码段是def、class、lamda;
而if/elif/else、try/except/finally、for/while 并不能更改变量作用域. 示例略
2. 变量搜索路径是:本地变量 -> 上层变量
示例如下:
def accessOut():
print(outVar) outVar = 10
accessOut()
在上例中,def改变了变量的作用域. 当执行print(outVar)时, 当前层中没有找到变量outVar的定义, 所以到上一层中找, 找到了, 所以就
用这一层的outVar(值为10).当然如果在这一层中也没有找到outVar,那么就要继续到上一层中查找.
如果你感觉这篇文章写得太简略了,你可以参照这里,讲得很详细.
通过一个例子来进一步理解:
#!/usr/bin/python2.7
#File: demo.py
#Author: lxw
#Time: 2014-09-01 number = 5
def func0():
#It's OK to reference.
print number def func1():
#new local variable.
number = 10
print number def func2():
#global declaration.
global number
print number
number = 10
print number print "Before calling any function, number is {}".format(number)
print "Calling func0()----------"
func0()
print "Calling func1()----------"
func1()
print "After calling func1(), number is {}".format(number)
print "Calling func2()----------"
func2()
print "After calling func2(), number is {}".format(number)
Output:
lxw@ubuntu:~/Python/Practice$ python demo.py
Before calling any function, number is 5
Calling func0()----------
5
Calling func1()----------
10
After calling func1(), number is 5
Calling func2()----------
5
10
After calling func2(), number is 10
注意: 如果将func1改成下面的形式(注意与func0的对比):
def func1():
#new local variable.
print number
number = 10
print number
就会出现下面的错误提示:
UnboundLocalError: local variable 'number' referenced before assignment
在python2.7 和 python3.4上测试, 出现同样的上述结果.
Python Variable Scope的更多相关文章
- python variable scope 变量作用域
python 中变量的作用域经常让我感到很迷 In Python, on the other hand, variables declared in if-statements, for-loop b ...
- [翻译] Tensorflow中name scope和variable scope的区别是什么
翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...
- [TensorBoard] Name & Variable scope
TF有两个scope, 一个是name_scope一个是variable_scope 第一个程序: with tf.name_scope("hello") as name_scop ...
- [Ruby] Ruby Variable Scope
Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, lo ...
- tensorflow变量作用域(variable scope)
举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时候共享变量会无比复杂. 官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望 ...
- PHP Variable Scope
原文: https://phppot.com/php/variable-scope-in-php/ Last modified on March 24th, 2017 by Vincy. ------ ...
- Python中变量的作用域(variable scope)
http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...
- python作用域 scope
可以先看:http://www.cnblogs.com/youxin/p/3645734.html 几个概念:python能够改变变量作用域的代码段是def.class.lamda.if/elif/e ...
- [Python] Understand Scope in Python
Misunderstanding scope can cause problems in your application. Watch this lesson to learn how Python ...
随机推荐
- hdu1584 A strange lift (电梯最短路径问题)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 打包Cocos2d-xproject为PC项目
<1>第一步,得到总体的大.exe 1.复制cocos2d-x-2.2文件下的Release.win32文件侠到桌面. 2.将项目下的Resources里的资源拷贝到Release.win ...
- PILE读书笔记_标准I/O
在学习和分析标准I/O库的同时, 可以重点与Linux的I/O系统调用进行比较. stdin. stdout和stderr都是FILE类型的文件指针, 是由C库静态定义的, 直接与文件描述符0. 1和 ...
- 基于windows的resin配置
Resin 与 Eclipse for JavaEE 的整合方法: 1.新建一个项目,将web application配置到resin.conf中 附上resin_struts2-111.conf文件 ...
- MapReduce源码分析之JobSubmitter(一)
JobSubmitter,顾名思义,它是MapReduce中作业提交者,而实际上JobSubmitter除了构造方法外,对外提供的唯一一个非private成员变量或方法就是submitJobInter ...
- IOS设计模式的六大设计原则之单一职责原则(SRP,Single Responsibility Principle)
定义 就一个类而言,应该仅有一个引起它变化的原因. 定义解读 这是六大原则中最简单的一种,通俗点说,就是不存在多个原因使得一个类发生变化,也就是一个类只负责一种职责的工作. 优点 类的复杂度降低,一个 ...
- 关于fork()父子进程返回值的问题
我们都知道,父进程fork()之后返回值为子进程的pid号,而子进程fork()之后的返回值为0.那么,现在就有一个问题了,子进程fork()的返回值是怎么来的?如果子进程又执行了一遍fork()函数 ...
- Consul实现原理系列文章1: 用Raft来实现分布式一致性
工作中用到了Consul来做服务发现,之后一段时间里,我会陆续发一些文章来讲述Consul实现原理.在前一篇文章中,我介绍了Raft算法.这篇文章会讲讲Consul是如何使用Raft算法来实现分布式一 ...
- POJ2947 DAZE [Gauss]
题目是要求建立一个方程组: (mat[1][1]*x[1] + mat[1][2]*x[2] + - + mat[1][n]*x[n])%7 =mat[1][n+1] (mat[2][1]*x[1] ...
- 【泰语歌】กลับคำสาหล่า 歌手:Mike Piromporn
Darling,Please don't leave me. 很短的一句话,可能会摧毁我们在一起的所有美好时光.