写笔记的第二天,另外希望自己能慢慢打动自己的女神누나

昨天,突然遇到了一个在函数内部修改全局变量却报错的问题,在网上查了下发现是经典问题,记一下日后慢慢消化。

提醒自己,函数内部修改全局变量时要思考三遍,是否真的要修改一个代码段内都要用到的全局变量!

D = ''
def group1(a,b):for c in range(4):C ='''%d=>array('limit'=>60,'id'=>%d,),'''%(lv[c],int(ID[a][c][b]))D = D + Creturn(D)
Traceback (most recent call last):File "/Users/bowenpang/PycharmProjects/untitled/cookingstand.py", line 271, in group1D = D + C
UnboundLocalError: local variable 'D' referenced before assignment

我在函数外创建了全局变量D,并且准备利用函数来修正D的值时直接报了错。
跑到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.


特意记下这个问题,在python里,如果在函数内部试图去修改一个全局变量,那么实际上的操作是
Python会创建一个新的名字一模一样的局部变量,并且在函数内部屏蔽(shadow)掉这个全局变量(为了保护),但是出了这个函数并不改变全局变量的值
如果想要让上面这段程序不报错,要不然不在函数中进行修改全局变量的操作,要不然去使用global函数,试了以下2种方式都能得到正确的结果。

D = ''
def group1(a,b):global Dfor c in range(4):C ='''%d=>array('limit'=>60,'id'=>%d,),'''%(lv[c],int(ID[a][c][b]))D = D + Creturn(D)
def group1(a,b):D = ''for c in range(4):C ='''%d=>array('limit'=>60,'id'=>%d,),'''%(lv[c],int(ID[a][c][b]))D = D + Creturn(D)

实际上,由于我命名的全局变量仅在函数中应用,全局只想调用这个函数的返回值,因此完全没有意义去定义并且修改一个全局变量,可能会导致自己的代码维护成本变高很多。因此,下次碰到修改全局变量这种情况,需要思考是否真的要修改全局变量。

UnboundLocalError: local variable referenced before assignment相关推荐

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

    1 val = 9 2 def test(flag): 3 if flag: 4 val = 1 5 else: 6 print("test") 7 return val 8 9 ...

  2. Python 引用全局变量提示:local variable referenced before assignment. 问题解决办法,global使用方法介绍

    local variable 'a' referenced before assignment 就是说变量a在使用前没有被声明 可能的情况一般有两种: 情况一:变量没有被赋值直接引用了 def hel ...

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

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

  4. 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 ...

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

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

  6. python 错误--UnboundLocalError: local variable 'num' referenced before assignment

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

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

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

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

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

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

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

最新文章

  1. 一份很不错的敏捷产品接口文档模板
  2. linux 下清空tomcat catalina.out内容,释放磁盘空间
  3. 信号处理之频谱原理与python实现
  4. jdbc java例子_Spring JDBC 例子
  5. 问题 D: 巧求和(思维)
  6. 『JavaScript』核心
  7. HTTPS原理和对中间件攻击的预防
  8. 单片机读tf卡c语言程序,单片机读写U盘闪盘超精简C源程序
  9. 使用java 遍历文件夹
  10. 关于Kafka中的再均衡
  11. javascript:void(0)
  12. UVA-12661 Funny Car Racing (dijkstra)
  13. STM32-关于Proteus 仿真无法运行STM32CubeMX自动生成的代码
  14. linux系统剪切,Linux 系统裁剪
  15. html圣诞节网页制作,圣诞节快乐——为你的网页添加一个可爱的HTML5圣诞节雪人!...
  16. debian安装vim提示Package vim is not available, but is referred to by another package的解决方案
  17. 五星级洒店系统需求分析
  18. 什么是迭代式项目开发
  19. 富景中国在港交所招股书失效,富景农业冲刺香港上市又一次折翼
  20. java jdk12_JDK 12:Java 12中的新功能

热门文章

  1. python --搭建FTP服务器
  2. 甘肃教育网计算机一级查询系统,甘肃计算机一级考试成绩查询
  3. SpringBoot 2.0+ /springframwork 5 自带的websocket, 实现后台主动推消息给前端,及前端发消息给后台(全双工模式通信)
  4. 交付工程师应掌握的知识点
  5. OA工作流引擎设计(纯java开发,不需要额外任何插件引擎)
  6. 基于自研分布式内存及流数据库技术的柏睿数据宣布获2亿元C轮融资
  7. 华为云桌面每台服务器应发布虚拟机台数计算
  8. 百度Ueditor项目中使用,配置
  9. mysql group by 组内排序方法
  10. 神经网络学习小记录40——春节到了,用LSTM写古诗不?