[Pythonmac-SIG] Bug in list + tupple?

Dan Grassi dan@grassi.org
Fri, 28 Jun 2002 21:37:57 -0400


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