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

  • Thread starter Gabriel Rossetti
  • Start date
G

Gabriel Rossetti

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
 
R

Richard Thomas

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

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

Richard.
 
B

Bruno Desthuilliers

Richard Thomas a écrit :
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...
 
H

Hrvoje Niksic

Richard Thomas said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top