Prev: Any Swisses here?
Next: Can't Add Variable
From: Gabriel Genellina on 4 Jan 2010 02:59 Is there any reason for this error? Apart from "nobody cared to write the code" py> [1,2,3] + (4,5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "tuple") to list In-place addition += does work: py> a = [1,2,3] py> a += (4,5) py> a [1, 2, 3, 4, 5] -- Gabriel Genellina
From: Steven D'Aprano on 4 Jan 2010 03:22 On Mon, 04 Jan 2010 04:59:02 -0300, Gabriel Genellina wrote: > Is there any reason for this error? Apart from "nobody cared to write > the code" Yes, because such implicit conversions would be a bad idea. > py> [1,2,3] + (4,5) What result are you expecting? A list or a tuple? > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: can only concatenate list (not "tuple") to list Apart from the different error message, this is essentially the same error as this: >>> 2 + "2" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' > In-place addition += does work: > > py> a = [1,2,3] > py> a += (4,5) > py> a > [1, 2, 3, 4, 5] I call that an impressive gotcha. I believe that is because in-place addition of lists is implemented as functionally equivalent to the extend method: >>> a += "abc" # same as a.extend("abc") >>> a [1, 2, 3, 4, 5, 'a', 'b', 'c'] >>> a += {None: -1} >>> a [1, 2, 3, 4, 5, 'a', 'b', 'c', None] -- Steven
From: Gabriel Genellina on 4 Jan 2010 03:27 En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <clp2(a)rebertia.com> escribi�: > On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina > <gagsl-py2(a)yahoo.com.ar> wrote: >> py> [1,2,3] + (4,5) >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> TypeError: can only concatenate list (not "tuple") to list Sorry, I inadvertedly posted an incomplete message. Note the last part: >> In-place addition += does work: >> >> py> a = [1,2,3] >> py> a += (4,5) >> py> a >> [1, 2, 3, 4, 5] > Given that tuples are sometimes used as a poor man's object (i.e. > collection of data fields), whereas lists are not typically used that > way, I'd say it's probably a good thing an explicit type conversion is > required here. In that case += should not be allowed either... -- Gabriel Genellina
From: David Williams on 4 Jan 2010 03:20 > Is there any reason for this error? Apart from "nobody cared to write the > code" > > py> [1,2,3] + (4,5) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: can only concatenate list (not "tuple") to list > > In-place addition += does work: > > py> a = [1,2,3] > py> a += (4,5) > py> a > [1, 2, 3, 4, 5] > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > They are different types.
From: David Williams on 4 Jan 2010 03:24
> Is there any reason for this error? Apart from "nobody cared to write the > code" > > py> [1,2,3] + (4,5) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: can only concatenate list (not "tuple") to list > > In-place addition += does work: > > py> a = [1,2,3] > py> a += (4,5) > py> a > [1, 2, 3, 4, 5] > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > I guess to expand a bit more on what I said... What should the result be? A list or a tuple? The reason += works is because the end result is clear; a list. But it is ambiguous in the case of concatenation: did you want a tuple or a list? |