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

Discussion in 'Python' started by Gabriel Rossetti, Dec 9, 2009.

  1. 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
    Gabriel Rossetti, Dec 9, 2009
    #1
    1. Advertising

  2. Re: UnboundLocalError: local variable '_[1]' referenced beforeassignment

    On Dec 9, 10:17 am, Gabriel Rossetti <>
    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 = [ 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.
    Richard Thomas, Dec 9, 2009
    #2
    1. Advertising

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

    Richard Thomas a écrit :
    > On Dec 9, 10:17 am, Gabriel Rossetti <>
    > 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...
    Bruno Desthuilliers, Dec 9, 2009
    #3
  4. Re: UnboundLocalError: local variable '_[1]' referenced before assignment

    Richard Thomas <> 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.
    Hrvoje Niksic, Dec 9, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Moscatt
    Replies:
    2
    Views:
    9,812
    Peter Moscatt
    Mar 19, 2005
  2. Replies:
    2
    Views:
    5,295
    Diez B. Roggisch
    Feb 26, 2006
  3. Wang, Harry
    Replies:
    2
    Views:
    311
  4. ch1zra
    Replies:
    9
    Views:
    3,098
    danieldelay
    Jun 8, 2010
  5. Replies:
    9
    Views:
    92
    John Gordon
    Jun 24, 2013
Loading...

Share This Page