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. Atitit.自定义存储引擎的接口设计 api 标准化 attilax 总结  mysql

    Atitit.自定义存储引擎的接口设计 api 标准化 attilax 总结  mysql 1. 图16.1:MySQL体系结构1 2. 16.7. 创建表create()虚拟函数:2 3. 16.8 ...

  2. java - day13 - ImplementDemo

    接口实现.继承等关系的运用案例P.S: 强制转换,看引用变量指向的对象与目标数据间的关系.可运用 "引用变量 instanceof 目标数据" 来判断是否可用强转 package ...

  3. IE浏览器上传图片预览兼容(IE 7 8 9 10 11)

    $("#file_upload").change(function () { var $file = $(this); ]; var windowURL = window.URL ...

  4. 文件存储结构inode与RAM结构建立联系

    linux下一切皆文件,大致可分为以下几类:目录.普通文件.硬连接.软连接.字符设备.块设备.FIFO.Socket,其在物理存储体内存储按inode和数据块存储,inode代表元数据,是除实际数据外 ...

  5. abp 列表和明细页面的规则说明

    0>列表区域mainviewbody 1>列表头mainviewheader 名称 2>列表条件mainviewcodtionbody 条件区 3>列表工具栏mainviewt ...

  6. quantz入门和使用流程(转载)

    1.下载地址:http://quartz-scheduler.org/downloads/catalog http://quartz-scheduler.org/downloads/destinati ...

  7. MySQL - 统计每个月生日的人数

    Person表定义如下: create table person(id int primary key auto_increment, birthday datetime); Person 数据如下: ...

  8. 005 android jni 一个简单的报错

    在android中使用ndk开发需要使用到jni. 1. java.lang.UnsatisfiedLinkError: No implementation found for void com.fr ...

  9. spark通过合理设置spark.default.parallelism参数提高执行效率

    spark中有partition的概念(和slice是同一个概念,在spark1.2中官网已经做出了说明),一般每个partition对应一个task.在我的测试过程中,如果没有设置spark.def ...

  10. -webkit-transition: all .2s ease-in-out;

    W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...