python 错误--UnboundLocalError: local variable '**' 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‘ : onlineTimeStr}
 8     msgStr = json.dumps(msg)
 9     producer.produce(msgStr)
10     COUNT += 1

结果报该错误,解决办法是在函数中加一行,global COUNT 即可解决

python 错误--UnboundLocalError: local variable '**' referenced before assignment

时间: 2024-11-10 13:39:54

python 错误--UnboundLocalError: local variable '**' 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=

变量引用的错误:UnboundLocalError: local variable 'range' 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 'l' referenced before assignment

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

全局变量报错:UnboundLocalError: local variable 'xxxxx' 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 'a' 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'. 那么问题来,这是个闭包函数,为啥不能引用本地

jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath

将公共引入的文件放到common.jsp中,其他页面引入该jsp即可使用 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 +

python的UnboundLocalError: local variable &#39;xxx&#39; referenced before assignment

一.意思: 本地变量xxx引用前没定义. 二.错误原因 在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以如果有修改变量的值就会变成局部变量. 三.产生这个错误的场景 python代码:val=9def test(flag):      if flag:          val = 1      else:          print 'fuck'      return val test(0)错误提示:U

python: local variable &#39;xxx&#39; 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"

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)