python的UnboundLocalError: local variable 'xxx' referenced before assignment

一、意思

本地变量xxx引用前没定义。

二、错误原因

在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以如果有修改变量的值就会变成局部变量。

三、产生这个错误的场景

python代码:
val=9
def test(flag):  
    if flag:  
        val = 1  
    else:  
        print ‘fuck‘  
    return val

test(0)
错误提示:UnboundLocalError: local variable ‘val‘ referenced before assignment

解决方法:用global关键字来进行说明该变量是全局变量
python代码:
val=9
def test(flag):
    global val
    if flag: 
        val = 1 
    else: 
        print ‘test‘ 
    return val

test(0)

参考资料:http://www.uplook.cn/biancheng/107/1078875/

转载自:http://blog.csdn.net/joeblackzqq/article/details/35278665

python的UnboundLocalError: local variable 'xxx' referenced before assignment

时间: 2024-08-04 21:19:54

python的UnboundLocalError: local variable 'xxx' referenced before assignment的相关文章

【python】UnboundLocalError: local variable 'counter' referenced before assignment

http://passport.baidu.com/?business&un=%E4%BA%91%E9%BE%99%E5%8E%BF%5F%E5%B0%8F%E5%A7%90%5F%E6%89%BE#0 http://passport.baidu.com/?business&un=%E6%89%BE%5F%E6%B4%B1%E6%BA%90%E5%8E%BF%5F%E7%BE%8E%E5%A5%B3#0 http://passport.baidu.com/?business&un=

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"

全局变量报错:UnboundLocalError: local variable &#39;l&#39; referenced before assignment

总结: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局变量之前调用变量名称(如print sum),则引发Unbound-LocalError 在程序中设置的sum属于全局变量,而在函数中没有sum的定义,根据python访问局部变量和全局变量的规则:当搜索一个变量的时候,python先从局部作用域开始搜索,如果在局部作用域没有找到那个变量,那样python就在全局变量中找这个变量,如果找不到抛出异常(NAMEE

变量引用的错误:UnboundLocalError: local variable &#39;range&#39; referenced before assignment

1 class Battery(): 2 """一次模拟电动汽车电瓶的简单尝试""" 3 def __init__(self,battery_size=70): 4 self.battery_size = battery_size 5 # self.range =range 6 def describe_battery(self): 7 print(self.battery_size) 8 9 def get_range(self): 10 pr

全局变量报错:UnboundLocalError: local variable &#39;xxxxx&#39; referenced before assignment

daibuchong 参考:http://blog.csdn.net/my2010sam/article/details/17735159 http://blog.csdn.net/onlyanyz/article/details/45009697 http://blog.csdn.net/magictong/article/details/4464024 全局变量报错:UnboundLocalError: local variable 'xxxxx' referenced before ass

UnboundLocalError: local variable &#39;a&#39; referenced before assignment

首先,上一段代码: 1 def out(): 2 a=1 3 def inner(): 4 a+=1 5 print(a) 6 return inner 7 func = out() 8 func() 初略看上去没有什么问题,运行之后报错:UnboundLocalError: local variable 'a' referenced before assignment 翻译成中文:UnboundLocalError:在赋值之前引用的本地变量'a'. 那么问题来,这是个闭包函数,为啥不能引用本地

Python | local variable &#39;xxxx&#39; referenced before assignment

>>> def func(num): ... def func_in(): ... num += 1 ... print(num) ... return func_in ... >>> fun = func(10) >>> fun <function func.<locals>.func_in at 0x1034410d0> >>> fun() Traceback (most recent call last)

python 错误--UnboundLocalError: local variable &#39;**&#39; referenced before assignment

昨日想在python的一个函数中做一下发送次数的统计,需要用到全局变量,如下 1 COUNT = 0 2 3 def sendOneLineMsg(producer, listFromLine): 4 acSNStr = listFromLine[0] 5 macStr = listFromLine[1] 6 onlineTimeStr = listFromLine[2] 7 msg = {'clientMAC' : macStr, 'acSN' : acSNStr, 'onLineTime'

遇到local variable &#39;e&#39; referenced before assignment这样的问题应该如何解决

问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变量进行重新的声明 通常这样的问题对于python的程序员来说都是因为习惯了python2的语法,转移到python3中时,出现的错误.在Python3中,异常对象无法在异常块作用域外访问.(原因是在垃圾收集器运行且从内存中清理引用之前会在内存栈帧中保存一个引用周期)通常参考下面这个例子来做异常处理: