+= vs .extend (was: List comprehensions' ugliness)

Gerrit Holl gerrit at nl.linux.org
Fri Feb 7 15:37:50 EST 2003


Jp Calderone schreef op vrijdag  7 februari om 19:05:44 +0000:
>   If you find the unpredictable semantics of += for lists to be bad, I
> imagine you won't like this, either:
> 
>   x = ([],)
>   x[0] += [1]
> 
>   Be sure to examine x again, after the second line executes ;)

Wow. How come? What happens exactly? First it does the job and than
it throws an exception? Shouldn't an exception mean that the
statement/expression failed? Is this bad design or is this a consequence
of some logical rule? Which corner of the language is responsible
for this?

 69 >>> x=([],)
 70 >>> x[0] += [1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
 71 >>> x
([1],)
 72 >>> a=x[0]
 73 >>> a+=[1]
 74 >>> x
([1, 1],)

I think it's weird anyway, to have a += b not be the same
as a = a + b, but that's a choice.

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list