UnboundLocalError: local variable '_[1]' referenced before assignment
Dave Angel
davea at ieee.org
Wed Dec 9 16:52:13 EST 2009
Gabriel Rossetti wrote:
> Dave Angel wrote:
>> Gabriel Rossetti wrote:
>>> <div class="moz-text-flowed" style="font-family: -moz-fixed">Hello
>>> everyone,
>>>
>>> I get this error on python 2.6.1 on mac os x 10.6 :
>>>
>>> UnboundLocalError: local variable '_[1]' referenced before assignment
>>>
>>> here's the code that raises this:
>>>
>>> params = [ self.__formatData(paramProcFunc, query, p) for p in params ]
>>>
>>> what I don't get is that it worked on mac os x 10.5 (python 2.5.x)
>>> but it no longer works. I tried the following and it works :
>>>
>>> r = []
>>> for p in params:
>>> r.append(self.__formatData(paramProcFunc, query, p))
>>> params = r
>>>
>>> Does anyone understand what is going on here?
>>>
>>> Thank you,
>>> Gabriel
>>>
>>> </div>
>>>
>> Clearly you're not supplying enough context. The UnboundLocalError
>> is only raised inside a function (or method), and you only show one
>> line of that function.
>>
>> And in the second example, it's even worse, since you imply it's
>> top-level code by carefully unindenting everything.
>>
>> My *guess* is that you have a global variable params, which you're
>> looping on. And then you assign a local variable by the same name.
>> If you have an assignment anywhere in the function, that name is
>> considered a local, and if you reference it before you assign it,
>> it'll generate this error.
>>
>> Three possible fixes, depending on why you're doing this.
>>
>> 1) pass the params in to the function as an argument
>> 2) use a different name for the local thing you're building
>> 3) use a global statement to force the compiler to use the global one
>> and not create a local. (probably a bad idea)
>>
>> DaveA
>>
>
> Hello Dave,
>
> ok, you' re right about not showing enough:
>
> params = [ self.__formatData(paramProcFunc, query, p) for p in params ]
>
>
> where :
>
> paramProcFunc = "percent2Volume"
>
> def __formatData(self, func, query, data):
> return getattr(self._unitDataAbs, func)(self._unitCmdAbs,
> query, data)
>
> def percent2Volume(self, absCmds, query, percent):
> return query, int(round(percent / 100.0 * absCmds["volCeil"]))
>
>
> but I still don't see where the problem is and why it works with the
> explicit loop and not the list comp.
>
> Thanks,
> Gabriel
>
I don't see either; you still don't supply nearly enough context. You
don't show enough code for someone to actually try it, and since it's
not crashing on that line, you're clearly not showing the whole stack trace.
I made a few wild guesses about your code, and have something that
compiles and runs. But I'm on Windows XP, with Python 2.6.4, so your
mileage may vary.
import sys
print sys.version
class Dummy(object):
def __init__(self):
self._unitDataAbs = self
self._unitCmdAbs = {"volCeil":298}
def crash(self):
query = "this is a query"
params = [100,20,37,42]
paramProcFunc = "percent2Volume"
params = [ self.__formatData(paramProcFunc, query, p) for p in
params ]
return params
def __formatData(self, func, query, data):
return getattr(self._unitDataAbs, func)(self._unitCmdAbs, query,
data)
def percent2Volume(self, absCmds, query, percent):
return query, int(round(percent / 100.0 * absCmds["volCeil"]))
obj = Dummy()
p = obj.crash()
print p
When I run it, I get the following output:
2.6.4 (r264:75706, Nov 3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)]
[('this is a query', 298), ('this is a query', 60), ('this is a query',
110), ('this is a query', 125)]
You could try pasting the same code into a file on your system, and if
it crashes, then copy the full error stacktrace into a message.
If it doesn't, you need to either post your whole code (enough for
somebody to actually test), or simplify it till it doesn't crash. Then
the next-to-last change is the one that masks the problem.
More information about the Python-list
mailing list