[Tutor] Unexpected behavior of +=

Kent Johnson kent37 at tds.net
Thu Feb 16 00:10:10 CET 2006


Michael Broe wrote:
> I just discovered the following behavior, but can't find any  
> documentation about it:
> 
>  >>> list = []
>  >>> list = list + 'abc'
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> TypeError: can only concatenate list (not "str") to list
> 
> but:
> 
>  >>> list = []
>  >>> list += 'abc'
>  >>> list
> ['a', 'b', 'c']

There was a recent thread on c.l.py that started with exactly this 
question. The consensus seems to be that it is a mistake but oh well.
http://groups.google.com/group/comp.lang.python/browse_frm/thread/c96f9830c6919354/94cab5c3b99a9ffa?tvc=1#94cab5c3b99a9ffa

Kent



More information about the Tutor mailing list