1)下面这种情况是不会报错的:

>>> x = 10
>>> def bar():
...     print(x)
>>> bar()
10

(2)但是这种情况就会报UnboundLocalError的错误:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1

该代码会提示UnboundLocalError: local variable ‘x’ referenced before assignment。

Python FAQ给出的回答是:

This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in foo assigns a new value to x, the compiler recognizes it as a local variable. Consequently when the earlier print(x) attempts to print the uninitialized local variable and an error results.

即如果给一个变量分配一个值,则这个变量会被认为属于当前的代码块,会屏蔽外部代码块(全局)的相同名字的变量。因为(2)中的代码中x+=1,相当于给x分配了一个值,则编译器意识到x是一个局部变量,因为x在print后面被赋值,则在之前调用的print(x)试图访问一个未被初始化的局部变量而失败。

(3)解决方法:将变量声明为global的

>>> x = 10
>>> def foobar():
...     **global x**
...     print(x)
...     x += 1
>>> foobar()
10

【debug】UnboundLocalError local variable a referenced before assignment相关推荐

  1. 【报错】UnboundLocalError: local variable ‘XXX‘ referenced before assignment解决办法

    参考这篇 https://blog.csdn.net/YZL40514131/article/details/122082820

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

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

  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. python 错误--UnboundLocalError: local variable 'num' referenced before assignment

    val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...

  5. UnboundLocalError: local variable 'end_page' referenced before assignment

    UnboundLocalError: local variable 'end_page' referenced before assignment 错误信息是在指出一个在使用之前没有赋值的局部变量.在 ...

  6. UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法

    UnboundLocalError: local variable 'loss' referenced before assignment 文章目录 UnboundLocalError: local ...

  7. UnboundLocalError: local variable ‘a‘ referenced before assignment(Python报错解决)

    问题提出 使用Python编程的时候,要注意函数内可以访问全局变量,但不能更新(修改)其值. 比如: a = 10 def sum ( n ) :n += aprint ('a = ', a, end ...

  8. 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment

    总结: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局变量之前调用变量名称(如print sum),则引发Unbou ...

  9. UnboundLocalError: local variable ‘Num_fSu‘ referenced before assignment

    在计算中调用 函数 calculate_psd 的时候,报了如下错误 def calculate_psd(Num_Tai, np_Tai, nr_in_10, Num_sub, dt, Tai) -& ...

最新文章

  1. redux学习笔记归纳
  2. H3C交换机S5500系列恢复控制台登录口令
  3. python相册管理系统_Django实现一个相片管理系统01
  4. android新材料设计,android - 如何实现新材料BottomAppBar为BottomNavigationView - SO中文参考 - www.soinside.com...
  5. 收集SQLite中的时间日期函数[ZT]
  6. java mysql 输入数据库_如何用JAVA录入数据到SQL数据库
  7. 设计模式 ( 六 ) 建造者模式
  8. 【自动驾驶】PID实现轨迹跟踪
  9. 「POI2012」约会 Rendezvous
  10. 【舆情分析(5)】 情感倾向分析之鲁迅《祝福》里对祥林嫂(特定人物)的情感倾向
  11. 77.组合 | 40.组合总和II | 39.组合总和 | 784.字母大小写全排列
  12. 使用Zend studio+WAMP来调试Wordpress后台的PHP程序的一些非常关键的信息(原创)
  13. STM32 B3 B5 A13 A14 A15端口复用
  14. -XX:+UseParallelGC与 -XX:+UseParNewGC 区别
  15. 使用burp-suite对投票系统进行攻击
  16. 孙陶然:有态度就是进取
  17. 无线接入控制服务器(ac),无线AP控制器是什么?无线AP与无线AC的区别
  18. 刘备学Android目录
  19. 什么是DTO、VO、BO、PO、DO、POJO
  20. 红米note5解锁教程_红米Note 5A解锁BL教程_红米Note5A获取解锁码进行解锁

热门文章

  1. qt退出 整个应用程序_为什么要对整个应用程序使用功能组合
  2. macOS安装docker
  3. 搭建简易留言板过程中遇到的问题
  4. win7下安装linux(CentOS)过程中遇到的问题总结
  5. finalize到底是什么
  6. Zigbee网络架构+ZigBee的体系结构+理解zigbee节点的实现的案例+“51单片机” 和 “zigbee” 、 “cc2530芯片” 之间的关系+芯片cc2530
  7. 2014中国软件开发者调查
  8. NLP《Tranformer和Self-Attention》
  9. 每天进步一点点《ML - 线性回归》
  10. oracle中右表有过滤条件的left outer join