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. Redis_发布订阅(基础)

    目录 前言 生产者和消费者 发布和订阅 Java实现 注意 前言 随着业务复杂, 业务的项目依赖关系增强, 使用消息队列帮助系统降低耦合度.发布订阅(pub/sub)是一种消息通信模式,主要目的是解除 ...

  2. 李洪强-HEAD 和nil和NULL

  3. Jenkins安装和配置系列(阳光温暖了心情)

    转自:http://www.cnblogs.com/yangxia-test/category/668771.html Jenkins学习一:Jenkins是什么? Jenkins学习二:Jenkin ...

  4. 配置LANMP环境(3)-- 安装anmp前准备与实用软件安装

    一.安装配置vim 1.安装 yum -y install vim* 2.创建文件夹 mkdir -p /etc/vim/ 3.配置 vim ~/.vimrc  "插入模式时 光标的上下左右 ...

  5. https原理与实践

    HTTPS 原理与证书实践   分类: Web应用   1.1 网络安全知识 1.1.1 网结安全出现背景 网络就是实现不同主机之间的通讯,网络出现之初利用TCP/IP协议簇的相关协议概念,已经满足了 ...

  6. Java基础02 方法与数据成员(转载)

    对象中的数据成员表示对象的状态.对象可以执行方法,表示特定的动作. 此外,我们还了解了类(class).同一类的对象属于相同的类型(type).我们可以定义类,并使用该定义来产生对象.   调用同一对 ...

  7. OC 基础语法

    :Obect c 与 c 语言的区别 () 后缀名不一样,C语言是.c 结尾 ,OC 是 .h结尾. () 输出信息不同 C语言是用print() 输出,OC 是用NSLog输出. () NSLog会 ...

  8. Python 字典初始化dict()和{}

    参考:https://doughellmann.com/blog/2012/11/12/the-performance-impact-of-using-dict-instead-of-in-cpyth ...

  9. 推荐个WIN7下小巧的可转录声音的软件-Audio Record Wizard V6.99

    之前是XP上用的是 WaveCN 2.0.0.5,但这个软件好久没更新了,不支持WIN7 最终找到了Audio Record Wizard V6.99,尽管没 WaveCN 2.0.0.5好用,但也全 ...

  10. OpenCV学习笔记十四:opencv_objdetect模块

    一,简介: 该库用于目标检测.