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

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

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

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

常见的local variable 'x' referenced before assignment问题相关推荐

  1. python UnboundLocalError: local variable 'log_f' referenced before assignment 错误

    在写一个python程序,用finally处理异常的时候,报了"UnboundLocalError: local variable 'log_f' referenced before ass ...

  2. 成功解决local variable 'str' referenced before assignment

    成功解决local variable 'str' referenced before assignment 目录 解决问题 解决思路 解决方法 解决问题 local variable 'str' re ...

  3. UnboundLocalError: local variable ‘XXX‘ referenced before assignment解决办法

    一.举例: 计算a到10的和 sum=0 def func(a):while a<=10:sum+=aa+=1return sum print(func(9)) 运行结果: UnboundLoc ...

  4. 【ROS-Error】 Can‘t convert image: local variable ‘pil_mode‘ referenced before assignment

    rqt_bag ***.bag 报错: Can't convert image: local variable 'pil_mode' referenced before assignment 原因: ...

  5. python的UnboundLocalError: local variable 'xxx' referenced before assignment

    From: http://blog.sina.com.cn/s/blog_8d3652760101d01p.html 一.意思: 本地变量xxx引用前没定义. 二.错误原因 在于python没有变量的 ...

  6. Python函数定义变量报错:local variable ‘a‘ referenced before assignment

    Python 全局变量与global关键字 ​ 在Python的变量使用中,经常会遇到这样的错误: local variable 'a' referenced before assignment 它的 ...

  7. local variable 'xxx' referenced before assignment

    local variable 'xxx' referenced before assignment 定义了一个全局变量 createTableFlag ,Python的一个函数里面引用这个变量,并改变 ...

  8. 【debug】UnboundLocalError local variable a referenced before assignment

    1)下面这种情况是不会报错的: >>> x = 10 >>> def bar(): ... print(x) >>> bar() 10 (2)但是 ...

  9. python local variable 'xxx' referenced before assignment

    python local variable 'xxx' referenced before assignment 代码 1 # -*- coding:utf-8 -*- 2  import sys 3 ...

最新文章

  1. hdu 3652 B-number(数位DP)
  2. 有关子数组最大累加和的算法小结
  3. linux最常用的目录表示,Linux磁盘分区的命名方式和常用目录?【每日一个知识点第84期-Linux】...
  4. 任务栏网速监控工具NetSpeedMonitor
  5. OpenResty安装
  6. PCM设备的作用,为什么要选用PCM设备?
  7. PostgreSQL数据库修改sql表的方法汇总
  8. 二进制安装kubernetes v1.11.2 (第八章 kube-apiserver 部署)
  9. 当了几年程序员,是时候考虑以后的发展了
  10. 进程、线程、协程对比
  11. deepin下深度终端使用ssh-agent(xshell中的xagent功能)
  12. git快速入门 push/clone/reset/merge/切换分支全都有
  13. 如何让网页中文本框的内容只能读不能写
  14. Django Web开发【2】Django入门
  15. mysql字符集设置lampp_xampp下mariaDB数据库设置默认字符集utf8(Windows)
  16. 统计通话次数和时间的软件_通话时间统计app下载
  17. 【OBS】OBS Studio 的安装、参数设置和录屏、摄像头使用教程
  18. 递归算法之求x的n次方
  19. 梅科尔工作室深度学习第五课-CNN
  20. 日行一pwn:pwn1_sctf_2016

热门文章

  1. JS实现植物大战僵尸小游戏,代码记录及效果展示
  2. 2022-4-15 基于单片机的超声波测距报警
  3. python语言编写的DLL注入工具
  4. pandas读取列数不同的数据方法
  5. 神经网络控制系统的应用,中枢神经信息网络系统
  6. 批处理命令goto的使用
  7. rpc协议之hprose接口测试
  8. SecureCRSecureFX8.1中文绿色免安装版
  9. Python操作Excel的Xlwings教程(五)
  10. ChatGPT和文心一言分别推荐的CNV分析工具