syntax glitch with +=

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Thu Feb 20 18:45:30 EST 2003


> From: David Eppstein [mailto:eppstein at ics.uci.edu]
> 
> 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]

No. += *uses* extend, but they are not identical. Specifically, every augmented *assignment* operator includes an assignment as part of the operation.

In the case of a list, the assignment is redundant, but still occurs.

Tim Delaney





More information about the Python-list mailing list