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. c++ 逗号操作符重载

    Overload Operator Comma 首先看看think in c++ 给出的一个重载的样例 #include <iostream> using namespace std; c ...

  2. Mysql数据库分库备份,分表备份

    分库备份 #!/bin/sh DBPATH=/server/backup MYUSER=root MYPASS=oldboy123 SOCKET=/data/3306/mysql.sock MYCMD ...

  3. C++语言基础(9)-继承

    一.继承的基本语法 #include<iostream> using namespace std; //基类 Pelple class People{ public: void setna ...

  4. mysql官网下载页面

    http://dev.mysql.com/downloads/mysql/5.6.html#downloads

  5. Django数据库表的关联问题

    Django模型中,比较难以理解的要数表和表之间相关联的部分,下面主要说说外键-ForeignKey和ManyToManyField2个字段类型. 我们知道ForeignKey说的是“一对多”,那么问 ...

  6. codeforces 651a oysticks

      Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status De ...

  7. PCB焊接工艺

    1. 有铅焊接工艺    240~260℃. 2. BGA焊盘直径为球径80%.

  8. UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)

    解题报告 题意: 有一个旅游团如今去出游玩,如今有n个城市,m条路.因为每一条路上面规定了最多可以通过的人数,如今想问这个旅游团人数已知的情况下最少须要运送几趟 思路: 求出发点到终点全部路其中最小值 ...

  9. tomcat本身的lib目录都有哪些jar包

    1.tomcat下的lib目录,自己带有的jar包有:servlet.jar,tomcat-jdbc.jar,tomncat-dbcp.jar,jsp.jar等 2.tomcat下的lib目录,自己带 ...

  10. easyui datagrid onLoadSuccess加载两次。。

    今天使用EasyUI的datagrid时发现首次打开页面时onLoadSuccess方法执行了两次.后来发现主要问题是datagrid被初始化了两次.主要原因是一开始html中声明了dg为easyui ...