python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope
情况一: a 直接引用外部的,正常运行
def toplevel():
a = 5
def nested():
print(a + 2) # theres no local variable a so it prints the nonlocal one
nested()
return a
情况二:创建local 变量a,直接打印,正常运行
def toplevel():
a = 5
def nested():
a = 7 # create a local variable called a which is different than the nonlocal one
print(a) # prints 7
nested()
print(a) # prints 5
return a
情况三:由于存在 a = 7,此时a代表嵌套函数中的local a , 但在使用a + 2 时,a还未有定义出来,所以报错
def toplevel():
a = 5
def nested():
print(a + 2) # tries to print local variable a but its created after this line so exception is raised
a = 7
nested()
return a
toplevel()
针对情况三的解决方法, 在嵌套函数中增加nonlocal a ,代表a专指外部变量即可
def toplevel():
a = 5
def nested():
nonlocal a
print(a + 2)
a = 7
nested()
return a
python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope的更多相关文章
- python: local variable 'xxx' referenced before assignment
问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...
- 解决Python报错:local variable 'xxx' referenced before assignment(引)
解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...
- 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment
闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...
- [合集]解决Python报错:local variable 'xxx' referenced before assignment
a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...
- local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...
- local variable 'xxx' referenced before assignment(犯过同样的错)
这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...
- _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 ...
- 遇到local variable 'e' referenced before assignment这样的问题应该如何解决
问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变 ...
随机推荐
- 【VS开发】【图像处理】Pleora推出iPORT CL-U3外置抓帧器
全球领先的高性能视频接口产品供应商Pleora科技公司近日宣布推出可将Camera Link®摄像头转化为USB3Vision™摄像头的首个产品iPORT CL-U3外置抓帧器,树立了另一个行业里程碑 ...
- 注解@Slf4j的作用
lombok.extern.slf4j 代码: @Slf4j public class LogExample { } 产生以下代码: public class LogExample { private ...
- Synchronized底层优化(轻量级锁、偏向锁)(二)
一.重量级锁 上篇文章中向大家介绍了Synchronized的用法及其实现的原理.现在我们应该知道,Synchronized是通过对象内部的一个叫做监视器锁(monitor)来实现的.但是监视器锁本质 ...
- Linux c实现一个tcp文件服务器和客户端
总体需求:编写tcp文件服务器和客户端.客户端可以上传和下载文件. ================================================ 分解需求 客户端功能描述: 1)要 ...
- Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...
- 【转帖】AMD:未向合资企业THATIC发放后续芯片设计授权
AMD:未向合资企业THATIC发放后续芯片设计授权 https://www.cnbeta.com/articles/tech/854193.htm 海光和兆芯的CPU 都不靠谱啊. 在台北电脑展(C ...
- 数据库中的round()
Round函数返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果.可是当保留位跟着的即使是5,有可能进位,也有可能舍去,机会各50%.这样就会造成在应用程序中计算有误. 参数规范 语法 r ...
- Elasticsearch入门教程(二):Elasticsearch核心概念
原文:Elasticsearch入门教程(二):Elasticsearch核心概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:ht ...
- KL散度的理解(GAN网络的优化)
原文地址Count Bayesie 这篇文章是博客Count Bayesie上的文章Kullback-Leibler Divergence Explained 的学习笔记,原文对 KL散度 的概念诠释 ...
- npm学习(七)之如何发布包、更新发布包、删除发布包
前言 我们经常使用npm来下载别人的模块或者说包,那么我们如何将自己写的模块上传到npm呢? 了解npm政策 在开始之前,最好回顾一下npm的政策,以防您对站点礼仪.命名.许可或其他指导原则有疑问. ...