FW: Newbie list question

Quinn Dunkan quinn at yak.ugcs.caltech.edu
Fri Jul 13 20:51:49 EDT 2001


On Fri, 13 Jul 2001 12:53:34 -0500, Skip Montanaro <skip at pobox.com> wrote:
>    Mark> Does:
>
>    Mark> foo += 'c'
>
>    Mark> act like an append then?  I always assumed it was the same as:
>
>    Mark> foo = foo + 'c'
>
>    Mark> which obviously should raise a TypeError.  Here however it works,
>    Mark> in that it appends 'c' to the object to which foo refers and thus
>    Mark> to the object to which bar refers...

I just noticed that too.  Of course, I knew that (+=) on a list maps to
[].extend, but didn't realize that extend would accept a string while (+)
wouldn't.  It's a manifestation of the fact that both lists and strings are
both sequences and hence "sorta similar".

In this case, one version of the operation thinks of strings as lists of
characters, and the other thinks of them as strings ('TypeError: can only
concatenate list (not "string") to list').

This seems inconsistent to me.  Either [].extend('string') should raise a
TypeError or "[1, 2] + 'ab'" should evaluate to [1, 2, 'a', 'b'].



More information about the Python-list mailing list