def fun1():
x = 5
def fun2():
x *= 2
return x
return fun2()

如上代码,调用fun1()

运行会出错:UnboundLocalError: local variable 'x' referenced before assignment。

这是因为对于fun1函数,x是局部变量,对于fun2函数,x是非全局的外部变量。当在fun2中对x进行修改时,会将x视为fun2的局部变量,屏蔽掉fun1中对x的定义(所以此时会认为x未定义,C++中则不会这样);如果仅仅在fun2中对x进行读取(比如x1 = x * 2,此时会找到外层的x),则不会出现这个错误。

解决办法:使用nonlocal关键字

def fun1():
x = 5
def fun2():
nonlocal x
x *= 2
return x
return fun2() fun1()
Out[14]: 10 使用了nonlocal x后,在fun2()中就不再将x视为fun2的内部变量,fun1函数中对x的定义就不会被屏蔽.

倘若x被定义在全局位置,则需要使用global.

x = 5
def fun1():
def fun2():
global x
x *= 2
return x
return fun2()

参考文章:https://blog.csdn.net/zhuzuwei/article/details/78234748

常见的local variable 'x' referenced before assignment问题的更多相关文章

  1. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  2. 遇到local variable 'e' referenced before assignment这样的问题应该如何解决

    问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变 ...

  3. RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment

    Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...

  4. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  5. 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment

    闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...

  6. [合集]解决Python报错:local variable 'xxx' referenced before assignment

    a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...

  7. python: local variable 'xxx' referenced before assignment

    问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...

  8. local variable 'xxx' referenced before assignment(犯过同样的错)

    这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...

  9. _markupbase.py if not match: UnboundLocalError: local variable 'match' referenced before assignment,分析Python 库 html.parser 中存在的一个解析BUG

    BUG触发时的完整报错内容(本地无关路径用已经用 **** 隐去): **************\lib\site-packages\bs4\builder\_htmlparser.py:78: U ...

随机推荐

  1. Git 学习笔记--删除错误提交的commit

    如果不小心把错误的commit给commit了,可以对其进行撤销 1.使用git log查看commit日志,找到错误提交前一版本commit的哈希值; 2.使用git reset --hard co ...

  2. checkmysql.sh

    #!/bin/sh # add by lxr MYSQL_SOCK="/tmp/mysql.sock" MYSQL_PWD="qq139547" ARGS=1 ...

  3. Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪?(已解决)

    Linux(Ubuntu)使用   sudo apt-get install  命令安装软件的目录在哪? bin文件路径: /usr/bin 库文件路径: /usr/lib/  其它的图标啊什么的路径 ...

  4. HashMap和Hashtable的区别 2

    导读: 1 HashMap不是线程安全的 hastmap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值.HashMap允许null k ...

  5. C# Hashtable

    哈希表(Hashtable) 在.NET Framework中,Hashtable 是 System.Collections 命名空间提供的一个容器,用于处理和表现类似 key-value 的键值对, ...

  6. thinkphp5---如何使用公共类

    在进行项目开发的时候,有很多的类是前后台以及其他模块都会使用的,例如验证码,上传类,密码加密的类等以及一些其他的第三方类库,如何在项目中提取这些公共的类呢? 具体方法: 例如:我在这里定义上传的类,里 ...

  7. 自己的memcache类

    Mem类代码: class Mem {     //类型是memcache或memcached     private $type = 'Memcached';     //会话     privat ...

  8. 170824、storm 环境搭建

    /*********************storm 环境搭建 **********************/ 1 安装jdk1.7 2 安装zookeeper集群环境 3 安装Python2.6. ...

  9. Eclipse中代码格式化配置

    一.配置formatter 从Eclipse主菜单选择“窗口→首选项”,进入“代码格式化程序”设置页.如下图所示: 确认选择的是格式化配置是Eclipse [built-in]. 注意:编写好代码后需 ...

  10. 配对堆优化Dijkstra算法小记

    关于配对堆的一些小姿势: 1.配对堆是一颗多叉树. 2.包含优先队列的所有功能,可用于优化Dijkstra算法. 3.属于可并堆,因此对于集合合并维护最值的问题很实用. 4.速度快于一般的堆结构(左偏 ...