local variable 'xxx' referenced before assignment
来源:互联网 发布:数据分析之量化投资 编辑:程序博客网 时间:2024/05/21 00:19
local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assignment,代码如下:
错误的意思就是xxx这个变量在引用前还没有定义,这上面不是定义了么?但是后来我把xxx = 24这句去掉之后,又没问题了,后来想起python中有个global关键字是用来引用全局变量的,尝试了一下,果然可以了:
原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。
[End]
阅读全文
0 0
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- UnboundLocalError: local variable 'XXX' referenced before assignment
- local variable XXX referenced before assignment
- local variable 'xxx' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- 解决Python报错:local variable 'xxx' referenced before assignment
- python出错:local variable XXX referenced before assignment
- Python进阶(三十二)-local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- 错误:UnboundLocalError: local variable 'xxx' referenced before assignment
- Python - UnboundLocalError: local variable referenced before assignment
- UnboundLocalError: local variable 'l' referenced before assignment
- Noip 2003 加分二叉树
- java解析xml文档的几种方式,概论
- C++ 程序崩溃调用堆栈打印
- 高端智能冰箱成白电市场新宠,营造品质生活方式为上策
- 一图囊括所有ES6知识点结构
- local variable 'xxx' referenced before assignment
- 适配器模式
- SSH框架搭建配置
- 总结提高关键词排名最全的41个技巧
- LeetCode-22. Generate Parentheses
- Redis使用示例
- Python爬虫入门:Urllib库的高级使用
- UGUI坐标与世界坐标的转换
- java Excel解析