syntax glitch with +=
Irmen de Jong
irmen at -NOSPAM-REMOVE-THIS-xs4all.nl
Thu Feb 20 17:57:47 EST 2003
David Eppstein wrote:
> If L and M are lists, L += M is the same as L.extend(M), right?
> Nope:
>
> L=[]
>
> def test1():
> print L
> L.extend([1,2])
>
> def test2():
> print L
> L += [3,4]
>
> One of these two works, the other produces an error.
You're getting
UnboundLocalError: local variable 'l' referenced before assignment
right? This error has nothing to do with a presumed difference
between extend and +=, in this case.
You need to add a 'global' statement. Try:
L=[]
def test1():
global L
print L
L.extend([1,2])
def test2():
global L
print L
L += [3,4]
Irmen
More information about the Python-list
mailing list