[Python-bugs-list] [ python-Bugs-575536 ] Concatenating a tuple to a list

noreply@sourceforge.net noreply@sourceforge.net
Sun, 30 Jun 2002 09:36:37 -0700


Bugs item #575536, was opened at 2002-06-29 19:18
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=575536&group_id=5470

Category: Python Interpreter Core
>Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Dan Grassi (dgrassi)
Assigned to: Tim Peters (tim_one)
Summary: Concatenating a tuple to a list

Initial Comment:
"a=a+b" is not the same as "a+=b" if a is a list and b is a tuple.  See the code below.

This has been tested on 2.2.1 MacPython, 2.2 MachoPython and 2.2 Python on an Alpha.

Augmented assignment (+=) with a list on the LHS allows a tuple on the RHS.  Standard assignment does not.

This seems intuitively wrong.

--- test code ---
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

Below is the execution results of the above:

>>>
>>> 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,)
>>>


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-30 11:35

Message:
Logged In: YES 
user_id=80475

Confirmed.  The behavior of list_inplace_concat diverged 
from list_concat.

See attached patch. Recommend applying to Py2.3 only 
since inconsistency isn't a bug.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=575536&group_id=5470