[Pythonmac-SIG] Bug in list + tupple?

Jack Jansen Jack.Jansen@oratrix.com
Sun, 30 Jun 2002 00:47:39 +0200


I'm not sure it's a bug, but it's definitely anti-intuitive. 
Apparently the += operator on lists is happy with any RHS that 
is a sequence while the + operator (from the list LHS) requires 
a list RHS.

I suggest you submit a bug report. If there is a good reason for 
the difference it should at least be clearly documented.

On zaterdag, juni 29, 2002, at 03:37 , Dan Grassi wrote:

> I noticed that "a=a+b" is not the same as "a+=b" if a is a list 
> and b is a tuple.  See the code below.
>
> I have tested this on 2.2.1 MacPython, 2.2 MachoPython and 2.2 
> Python on an Alpha.
>
> Is this correct behavior?  It seems intuitively wrong to me.
>
> a=[1]
> b=(2,)
>
> print 1,type(a), a
> print 1,type(b), b
> c=a+b
>
> print 2,type(a), a
> print 2,type(b), b
> a=a+b
>
> print 3,type(a), a
> print 3,type(b), b
> a+=b
>
> print 4,type(a), a
> print 4,type(b), b
>
> When I run this program I get:
>
> >>>
> >>> a=[1]
> >>> b=(2,)
> >>>
> >>> print 1,type(a), a
> 1 <type 'list'> [1]
> >>> print 1,type(b), b
> 1 <type 'tuple'> (2,)
> >>> c=a+b
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: can only concatenate list (not "tuple") to list
> >>>
> >>> print 2,type(a), a
> 2 <type 'list'> [1]
> >>> print 2,type(b), b
> 2 <type 'tuple'> (2,)
> >>> a=a+b
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: can only concatenate list (not "tuple") to list
> >>>
> >>> print 3,type(a), a
> 3 <type 'list'> [1]
> >>> print 3,type(b), b
> 3 <type 'tuple'> (2,)
> >>> a+=b
> >>>
> >>> print 4,type(a), a
> 4 <type 'list'> [1, 2]
> >>> print 4,type(b), b
> 4 <type 'tuple'> (2,)
> >>>
>
> Dan
>
>
>
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
>
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- 
Emma Goldman -