From: Gabriel Genellina on
En Mon, 04 Jan 2010 05:24:56 -0300, David Williams <david(a)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

From: Gabriel Genellina on
En Mon, 04 Jan 2010 05:22:44 -0300, Steven D'Aprano
<steven(a)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

From: Steven D'Aprano on
On Mon, 04 Jan 2010 06:27:48 -0300, Gabriel Genellina wrote:

> En Mon, 04 Jan 2010 05:24:56 -0300, David Williams
> <david(a)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
From: Jean-Michel Pichavant on
Gabriel Genellina wrote:
> En Mon, 04 Jan 2010 05:24:56 -0300, David Williams
> <david(a)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


From: r0g on
Gabriel Genellina wrote:
> 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...
>


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.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Any Swisses here?
Next: Can't Add Variable