解决Python报错:local variable 'xxx' referenced before assignment(引)
2013-08-19 12:08
453 查看
解决Python报错:local variable 'xxx' referenced before assignment(引)
(2011-03-03 15:51:23)转载▼
标签: | 分类: 编程 |
view plaincopy
to clipboardprint?
xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
print strFileName
xxx = 24
PrintFileName("file")
错误的意思就是xxx这个变量在引用前还没有定义,这上面不是定义了么?但是后来我把xxx = 24这句去掉之后,又没问题了,后来想起python中有个global关键字是用来引用全局变量的,尝试了一下,果然可以了:
view plaincopy
to clipboardprint?
xxx = 23
def PrintFileName(strFileName):
global xxx
if xxx == 23:
print strFileName
xxx = 24
PrintFileName("file")
原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。
相关文章推荐
- 解决Python报错: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的UnboundLocalError: local variable 'xxx' referenced before assignment
- python出错:local variable XXX referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable XXX referenced before assignment
- local variable 'xxx' referenced before assignment
- 【Python】UnboundLocalError: local variable 'x' referenced before assignment
- 【Python基础】Python3: UnboundLocalError: local variable 'num' referenced before assignment
- [合集]解决Python报错:local variable 'xxx' referenced before assignment
- 【python】UnboundLocalError: local variable 'counter' referenced before assignment
- python - Local variable referenced before assignment
- local variable 'xxx' referenced before assignment
- 【Python基础】之全局变量(UnboundLocalError: local variable referenced before assignment)
- Python:UnboundLocalError: local variable 'num' referenced before assignment
- local variable 'xxx' referenced before assignment
- python 报错 :local variable 'totalCount' referenced before assignment