f



UnboundLocalError: local variable '_[1]' referenced before assignment

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
0
Gabriel
12/9/2009 10:17:08 AM
comp.lang.python 77058 articles. 4 followers. Post Follow

3 Replies
544 Views

Similar Articles

[PageSpeed] 25

On Dec 9, 10:17=A0am, Gabriel Rossetti <gabriel.rosse...@arimaz.com>
wrote:
> 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 =3D [ 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 =3D []
> for p in params:
> =A0 =A0 r.append(self.__formatData(paramProcFunc, query, p))
> params =3D r
>
> Does anyone understand what is going on here?
>
> Thank you,
> Gabriel

That isn't an error that should occur, not least because _[1] isn't a
valid name. Can you post a full traceback?

Richard.
0
Richard
12/9/2009 12:40:11 PM
Richard Thomas a �crit :
> On Dec 9, 10:17 am, Gabriel Rossetti <gabriel.rosse...@arimaz.com>
> wrote:
>>
>> UnboundLocalError: local variable '_[1]' referenced before assignment
>>
> 
> That isn't an error that should occur, not least because _[1] isn't a
> valid name

It's an internal identifier used in list comps. Implementation detail, 
really, you're not even supposed to know about it...

0
Bruno
12/9/2009 2:14:05 PM
Richard Thomas <chardster@gmail.com> writes:

> That isn't an error that should occur, not least because _[1] isn't a
> valid name. Can you post a full traceback?

The name _[n] is used internally when compiling list comprehensions.
The name is chosen precisely because it is not an (otherwise) valid
identifier.  For example, try:

import dis
dis.dis(lambda: [ a for a in l ])

The user should never see _[n]; if he does, it's probably due to a bug
in Python.
0
Hrvoje
12/9/2009 2:48:27 PM
Reply: