python: local variable 'xxx' referenced before assignment
问题发现
xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
print strFileName
xxx = 24 PrintFileName("file")
报错
Traceback (most recent call last):
File "C:/Users/Desktop/del.py", line 7, in <module>
PrintFileName("file")
File "C:/Users/jihite/Desktop/del.py", line 3, in PrintFileName
if xxx == 23:
UnboundLocalError: local variable 'xxx' referenced before assignment
意思说局部变量‘xxx’前边没有定义,但是最前面不是定义了吗。注意这里提示是局部变量,一开始定义的为全局变量。如果这里定义的就是全局变量可以通过关键字global来说明
xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
global xxx
print strFileName
xxx = 24 PrintFileName("file")
运行正常。
但是这样也是没错
xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
print xxx PrintFileName("file")
问题所在
在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。
python: local variable 'xxx' referenced before assignment的更多相关文章
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...
- 解决Python报错:local variable 'xxx' referenced before assignment(引)
解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...
- [合集]解决Python报错:local variable 'xxx' referenced before assignment
a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...
- Python UnboundLocalError: local variable 'xxx' referenced before assignment 解决方法
一.报错含义: val=9 def test(): print(val) val = 6 print(val) test() 翻译:本地变量xxx引用前没有定义. 二.报错原因 这是Python变量作 ...
- python:UnboundLocalError: local variable 'xxx' referenced before assignment
近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...
- local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...
- local variable 'xxx' referenced before assignment(犯过同样的错)
这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...
- python的UnboundLocalError: local variable 'xxx' referenced b
一.意思: 本地变量xxx引用前没定义. 二.错误原因 在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以 ...
- python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope
情况一: a 直接引用外部的,正常运行 def toplevel(): a = 5 def nested(): print(a + 2) # theres no local variable a so ...
随机推荐
- 【模板模式】 Template Pattern
模板模式 又叫模板方法模式,在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情冴下,重新定义算法中的某些步骤(这个我觉得很抽象,很抽象) e:学会说“不 ...
- C#面向对象:多态
此文章转载网站:https://www.cnblogs.com/qixinbo/p/8244583.html 多态: 有多态之前必须要有继承,只有多个类同时继承了同一个类,才有多态这样的说法. 在继承 ...
- 异步编程async/await
什么是异步? 在异步程序中,程序代码不需要按照编写时的顺序严格执行,有时需要一在一个新的线程中运行一部分代码,有时无需创建新的 线程,但是为了更好的利用单个线程的能力,需要改变代码的执行顺序. 进程 ...
- Java之static静态代码块
Java之static静态代码块 构造代码块 使用{}包裹的代码区域,这里的代码区域特指位于class{}下面的而不是存在于其他type method(){}这类函数下面的代码区域 public cl ...
- pc端美化select,jquery获取select中的option的text值
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- java 程序设计第一次作业
public class Join{ public static void main(String args[]){ String s1=new String("hello"); ...
- 微信小程序之tabbar切卡
最近在研究小程序的时候,遇到了一个问题,就是tabbar切卡,在android上有fragment,在RN上也有提供一个第三方的组件来用,微信小程序,好像没有专门的一个组件来实现这个功能,度娘了大半天 ...
- js导出页面的表格到excel(NB的大神洗了好几个,挑一个记下来)
var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.in ...
- npm淘宝镜像和默认镜像切换
1.得到原本的镜像地址 npm get registry > https://registry.npmjs.org/ 设成淘宝的 npm config set registry http://r ...
- ubuntu16.04 能启动mysql服务
ubuntu16.04 后, 貌似mysqld在/etc/init.d下,直接执行会报mysqld不在服务中,因此开启mysql服务失败. 所以执行以下命令不能启动mysql服务: /etc/init ...