解决Python报错:local variable 'xxx' referenced before assignment(引)

解决Python报错:local variable 'xxx' referenced before assignment(引)

 

这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assignment,代码如下:

  1. xxx = 23
  2. def PrintFileName(strFileName):
  3. if xxx == 23:
  4. print strFileName
  5. xxx = 24
  6. PrintFileName("file")

错误的意思就是xxx这个变量在引用前还没有定义,这上面不是定义了么?但是后来我把xxx = 24这句去掉之后,又没问题了,后来想起python中有个global关键字是用来引用全局变量的,尝试了一下,果然可以了:

  1. xxx = 23
  2. def PrintFileName(strFileName):
  3. global xxx
  4. if xxx == 23:
  5. print strFileName
  6. xxx = 24
  7. PrintFileName("file")

原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。

解决Python报错:local variable 'xxx' referenced before assignment(引)的更多相关文章

  1. Python UnboundLocalError: local variable 'xxx' referenced before assignment 解决方法

    一.报错含义: val=9 def test(): print(val) val = 6 print(val) test() 翻译:本地变量xxx引用前没有定义. 二.报错原因 这是Python变量作 ...

  2. local variable 'xxx' referenced before assignment(犯过同样的错)

    这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...

  3. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  4. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  5. python: local variable 'xxx' referenced before assignment

    问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...

  6. [合集]解决Python报错:local variable 'xxx' referenced before assignment

    a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...

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

  8. python:UnboundLocalError: local variable 'xxx' referenced before assignment

    近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...

  9. 【Error】local variable 'xxx' referenced before assignment

    此种错误涉及到变量的作用域,即全局变量和局部变量的操作. 总结如下: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局 ...

随机推荐

  1. [CSP-S模拟测试]:影魔(树状数组+线段树合并)

    题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...

  2. python进阶:类和对象

    @修饰符:将被修饰的函数作为参数,运行修饰函数 实例方法: 静态方法:@staticmethod 类方法:@classmethod 自省:通过一定的机制查询到对象的内部结构 序列类: 列表推导式(例表 ...

  3. Insmod模块加载过程分析

    一.背景 a) 在进行JZ2440的一个小demo开发的时候,使用自己编译的内核(3.4.2)及lcd模块进行加载时,insmod会提示加载失败因为内核版本不匹配(提示当前内核版本为空),并且显示模块 ...

  4. jsp中jstl、el使用

    tomcat7.0+JSTL1.1.2(不冲突) EL表达式获取变量 ${表达式} 如:${user.name} 不可以动态取值 ${user[name]}可以动态取值,变量名中含有特殊字符时只能用此 ...

  5. 微信小程序,获取二维码

    微信小程序,获取二维码 找到一篇很实用的博客,他已经写得很详细了,自己也懒得写,亲测有效 参考网址

  6. C++——右值引用

    1.绑定到一个将要销毁的右值的引用——移动 2.左值表达式——对象的身份:右值表达式——对象的值 ; int& r=i; int&& rr=i;//×,左值不能绑定到右值引用 ...

  7. shell 字符串匹配变量(只取数字或者取固定字符串)

    var1=abc3559   #想要获得3559 操作: var1_key=`echo $var1 | tr -cd "[0-9]"` https://www.cnblogs.co ...

  8. idea将springboot项目打成jar包

    1.首先,找到pom.xml,把下面的build块中的内容改成如下所示 <build> <plugins> <plugin> <groupId>org. ...

  9. XStream教程

    XStream是一个简单的基于Java库,Java对象序列化到XML,反之亦然(即:可以轻易的将Java对象和xml文档相互转换). 特点 使用方便 - XStream的API提供了一个高层次外观,以 ...

  10. js函数方法

    /** * call()和apply * 这两个方法都是函数对象的方法,需要通过函数对象来调用 * 当对函数调用call()和apply()都会调用函数执行 * 在调用call()和apply()可以 ...