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的更多相关文章

  1. python variable scope 变量作用域

    python 中变量的作用域经常让我感到很迷 In Python, on the other hand, variables declared in if-statements, for-loop b ...

  2. [翻译] Tensorflow中name scope和variable scope的区别是什么

    翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...

  3. [TensorBoard] Name & Variable scope

    TF有两个scope, 一个是name_scope一个是variable_scope 第一个程序: with tf.name_scope("hello") as name_scop ...

  4. [Ruby] Ruby Variable Scope

    Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, lo ...

  5. tensorflow变量作用域(variable scope)

    举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时候共享变量会无比复杂. 官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望 ...

  6. PHP Variable Scope

    原文: https://phppot.com/php/variable-scope-in-php/ Last modified on March 24th, 2017 by Vincy. ------ ...

  7. Python中变量的作用域(variable scope)

    http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...

  8. python作用域 scope

    可以先看:http://www.cnblogs.com/youxin/p/3645734.html 几个概念:python能够改变变量作用域的代码段是def.class.lamda.if/elif/e ...

  9. [Python] Understand Scope in Python

    Misunderstanding scope can cause problems in your application. Watch this lesson to learn how Python ...

随机推荐

  1. 迁移TFS 2012服务至新的电脑硬件

    迁移TFS 2012的时候碰到一些问题, 中文记录很少, 英文的记录也比较零散. 这里记录最直接和简单的方法. 环境: 1. 公司域环境, 所有TFS用户都是公司域帐户. 2. TFS从一台服务器转移 ...

  2. libevent源码学习_event_test

    对应的sample文件中提供了event_test.c,里面就是关于事件的简单示例,具体如下: /* * Compile with: * cc -I/usr/local/include -o even ...

  3. python 旧类中使用property特性的方法

    在python中,我们可以拦截对象的所有特性访问.通过这种拦截的思路,我们可以在旧式类中实现property方法. __getattribute__(self, name) #当特性name被访问时自 ...

  4. ansible学习之--安装Svn

    1.安装svn 机器 Ubuntu SMP Thu Jan 15 20:21:55 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 使用 sudo apt-get in ...

  5. X264参考手册

    艺搜简介 基本语法: x264 [options]-o outfile infile 注意与ffmpeg的输入输出文件位置恰好相反: ffmpeg[options][[infile options]- ...

  6. Easyui 基于kindeditor的扩展

    源码 /** * Author : ____′↘夏悸 * Easyui KindEditor的简单扩展. * 有了这个之后,你就可以像使用Easyui组件的方式使用KindEditor了 * 前提是你 ...

  7. Servlet 调试

    测试/调试 Servlet 始终是开发使用过程中的难点.Servlet 往往涉及大量的客户端/服务器交互,可能会出现错误但又难以重现. 这里有一些提示和建议,可以帮助您调试. System.out.p ...

  8. Eclipse 创建 XML 文件

    Eclipse 创建 XML 文件 打开新建 XML 文件向导 你可以使用新建 XML 文件向导来创建 XML 文件.打开向导的方式有: 点击 File 菜单并选择 New > Other 点击 ...

  9. CentOS 6.5 MySQL5.6.26源码安装

    一.源码安装cmake工具 从mysql5.5起,mysql源码安装开始使用cmake wget http://cmake.org/files/v3.2/cmake-3.2.3.tar.gztar z ...

  10. Python中的多进程与多线程/分布式该如何使用

    在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global interpreter lock(也被亲切的称为“GIL”)指指点点,说它阻碍了Python的多线程程序同时 ...