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 ...
随机推荐
- Python归并排序(递归实现)
为什么归并排序如此有用?1. 快捷和稳定归并排序成为⼀一个非常棒的排序算法主要是因为它的快捷和稳定.它的复杂度即使在最差情况下都是O(n log n).而快速排序在最差情况下的复杂度是O(n^2),当 ...
- UML类图详解_泛化关系
泛化其实就是继承关系,还是比较简单的,那么我们就把之前有些问题的博客UML类图重新来实现一次. 依旧是这个图 下面我们来看一个例子 Account.h #include <cstdlib> ...
- __attribute__系列之cleanup
cleanup属性:当变量离开它的作用域时,设置的cleanup_function函数将被调用. cleanup (cleanup_function) The cleanup attribute ru ...
- row format delimited fields terminated by ','
row format delimited fields terminated by ',' 以','结尾的行格式分隔字段
- 着手打造你的随身系统---将linux装进移动硬盘
将Ubuntu等linux系统安装到移动硬盘--操作系统随身携带 前言 刚刚接触ubuntu,听说可以将linux系统安装到移动硬盘上,所以最近一周都在尝试将ubuntu安装到新买的移动 ...
- fedora上安装sun jdk
系统被来就有openjdk,但是开发工具需要sun的jdk,于是下载一个压缩包并解压到一个位置.使用alternative命令切换 alternatives --.0_79/jre/bin/java ...
- Spring MVC复选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...
- Starting Tomcat v7.0 Server at localhost' has encountered a problem. 如何解决
刚刚学习JavaWeb,遇到了一个问题 因为是初学,我在工程中编写了一个JSP,想要在浏览器中打开,然后出现了这个问题: 在途中大家可以看到,我点击运行写好的JSP,却出现错误,真正的原因是因为Jav ...
- docker搭建lnmp环境(问题,资料,命令)
入门参考 http://www.runoob.com/docker/docker-install-nginx.html 十大常用命令玩转docker 1. #从官网拉取镜像 docker pull & ...
- windows下sshfs挂载远程文件夹-server could not connect故障解决
使用sshfs挂载server上面的文件夹到windows中.轻松方便. 在之前的系统上挂载.没出问题. 近期买了块固态硬盘.装了个系统. 结果在系统上执行sshfs时报例如以下错误: 事实上非常ea ...