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. 利用python对微信自动进行消息推送

    from wxpy import * #该库主要是用来模拟与对接微信操作的 import requests from datetime import datetime import time impo ...

  2. Jexus 安装asp.net mvc EF 项目引发的错误总

    1.Linux 中的文件路径问题(配置文件路径),必须使用左斜杆 “/” 2.MVC 看 View/Web.config 下的配置文件中版本不对报错,如下: Could not locate Razo ...

  3. vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑

    导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...

  4. python中的os

    import sys, os print(__file__) # 绝对路径,实际是文件名 /Users/majianyu/Desktop/test/bin/bin.py print(os.path.a ...

  5. Gif动态图UIImage

    #pragma mark 动态图生成 -(UIImage *)getAnimatedGIFWithData:(NSString *)path { NSData *data = [NSData data ...

  6. coreseek搜索

    参考文档地址:http://github.tiankonguse.com/doc/sphinx/

  7. Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案

    使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...

  8. [administrative][CentOS] 新装系统时如何正确精准的选择基础环境和软件包

    出于不同的目的,在进行全新CentOS安装的时候,我们到底应该如何作出选择. 是mininal,base server, base web server, 还是啥? 答案在这里: https://ac ...

  9. 斜率优化&单调性优化的相似性

    写了一道单调性优化发现 跟斜率优化很像,而且这道题目感觉质量非常的好. 其实斜率优化是基于单调性优化的,但是面对这道题 我竟然连单调性优化都不太会,尽管这个模型非常不好理解. 对于每道题 我都会打一个 ...

  10. el表达式获取对象属性值 返回值类型

    实现 数字页码时 遇到的一个问题. 后端servlet 在request.setAttribute("page",page); page 为pagebean的实例对象,pagebe ...