Is this a bug?
Robert Kern
rkern at ucsd.edu
Sun Apr 24 11:34:48 EDT 2005
Michael Sparks wrote:
> Hi,
>
>
> I've hit a corner case that I can explain to myself *why* it happens, both
> under python 2.3 and python 2.4, but the following inconsistency makes me
> wonder if I should log it as a bug:
>
> First the behaviour that isn't unexpected:
>
>
>>>>a=["hello"]
>>>>a = a + "world"
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: can only concatenate list (not "str") to list
>
>
> Which is pretty what I'd expect.
>
> However if we do this just slightly differently:
>
>
>>>>a = ["hello"]
>>>>a += "world"
>>>>a
>
> ['hello', 'w', 'o', 'r', 'l', 'd']
>
> We get completely different behaviour. This strikes me as a bug - should I
> log it as one, or is there a good reason for this behaviour?
It's consistent with using a.extend("world") which is what the += is
sugar for.
In [1]:a = ['hello']
In [2]:a.extend("world")
In [3]:a
Out[3]:['hello', 'w', 'o', 'r', 'l', 'd']
It's a *good* thing that .extend() takes any iterable without explicit
conversion to a list. I think that it's just a minor annoyance that the
behavior passes on to +=.
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list