Re: TypeError: can only concatenate list (not tuple) to list
On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: This 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 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. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert c...@rebertia.com escribió: On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina gagsl-...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
En Mon, 04 Jan 2010 05:24:56 -0300, David Williams da...@bibliolabs.com escribió: 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] 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? Uhm... it seems obvious to me that [1,2,3] + (4,5) should be [1,2,3,4,5]. A list. That's what I would expect, although I cannot explain why is it *so* obvious to me. Given that 2 + 3.5, and 'abc' + u'def' both return an instance of their right operand's type, I should probably revise my preconceptions... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
En Mon, 04 Jan 2010 05:22:44 -0300, Steven D'Aprano ste...@remove.this.cybersource.com.au escribió: 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. I'm slowly convincing myself that it was actually a bad idea... 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] So += and extend are completely permissive - they slurp whatever comes from iterating their right operand. Totally unexpected in some cases, as in your examples above... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
On Mon, 04 Jan 2010 06:27:48 -0300, Gabriel Genellina wrote: En Mon, 04 Jan 2010 05:24:56 -0300, David Williams da...@bibliolabs.com escribió: 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] 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? Uhm... it seems obvious to me that [1,2,3] + (4,5) should be [1,2,3,4,5]. A list. That's what I would expect, although I cannot explain why is it *so* obvious to me. If you can't explain it, it's probably a bad idea. Why should lists be privileged over tuples? How does it work? Do tuples automatically turn into lists, or does the left hand value over-ride the right hand value? In other words, would you expect: 1 + 1 = 11 1 + 1 = 2 or would you expect: 1 + 1 = 11 1 + 1 = 11 Given that 2 + 3.5, and 'abc' + u'def' both return an instance of their right operand's type, I should probably revise my preconceptions... Yes, you should be a little more careful in your tests. 2 + 3.5 5.5 3.5 + 2 5.5 Nothing to do with left versus right. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
Gabriel Genellina wrote: En Mon, 04 Jan 2010 05:24:56 -0300, David Williams da...@bibliolabs.com escribió: 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] 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? Uhm... it seems obvious to me that [1,2,3] + (4,5) should be [1,2,3,4,5]. A list. That's what I would expect, although I cannot explain why is it *so* obvious to me. Given that 2 + 3.5, and 'abc' + u'def' both return an instance of their right operand's type, I should probably revise my preconceptions... Haa... The well known 'obviousness theorem' :o) by which everything that I say becomes true. Best theorem ever. I'm really surprised that s = [1,2,3] ; s += (4,5) works fine. Semantically, it is adding 2 operands of different type, this is not very smart. As variales have no predefined type and can change over statements, it would be unwise to assume that s += (4,5) should produce a list. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
Gabriel Genellina wrote: En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert c...@rebertia.com escribió: On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina gagsl-...@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... I'd be strongly inclined to think the result would be the sequence on the left with the data from the second sequence appended to it. What's wrong with a little duck typing here eh? Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
On Tue, 05 Jan 2010 00:52:56 +, r0g wrote: I'd be strongly inclined to think the result would be the sequence on the left with the data from the second sequence appended to it. What's wrong with a little duck typing here eh? That's not the existing behaviour. List concatenation doesn't mutate the left hand list, it creates a new list: L = [1, 2, 3] L2 = L + [4, 5, 6] L [1, 2, 3] L2 [1, 2, 3, 4, 5, 6] But if you insist on in-place modification, why do you prefer appending the right hand sequence to the left instead of prepending the left hand sequence to the right? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: can only concatenate list (not tuple) to list
Steven D'Aprano wrote: On Tue, 05 Jan 2010 00:52:56 +, r0g wrote: I'd be strongly inclined to think the result would be the sequence on the left with the data from the second sequence appended to it. What's wrong with a little duck typing here eh? OK, I hadn't read all the other responses when I posted, some of which make fair points why this wouldn't be wise. Fair enough. That's not the existing behaviour. List concatenation doesn't mutate the left hand list, it creates a new list: I would expect it to append. That's my prejudice though, as I do that far more often :/ L = [1, 2, 3] L2 = L + [4, 5, 6] L [1, 2, 3] L2 [1, 2, 3, 4, 5, 6] But if you insist on in-place modification, why do you prefer appending the right hand sequence to the left instead of prepending the left hand sequence to the right? In-place seems more natural for a mutable type. I admit the left right thing is my prejudice though, western cultural bias I suppose. Its not entirely unprecedented though, the parser reads left to right and the leftmost terms take precedent in lazy logic evaluation. Still, the responses to this have convinced me the + operator shouldn't make assumptions, I'm more open to how += works though as it implies in-place and the left over right precedent quite nicely. Roger. -- http://mail.python.org/mailman/listinfo/python-list