[Python-bugs-list] [ python-Bugs-475678 ] y += x versus y = y+x surprise

noreply@sourceforge.net noreply@sourceforge.net
Sat, 27 Oct 2001 20:22:48 -0700


Bugs item #475678, was opened at 2001-10-27 20:22
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=475678&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 7
Submitted By: Tim Peters (tim_one)
Assigned to: Nobody/Anonymous (nobody)
Summary: y += x versus y = y+x surprise

Initial Comment:
This report from c.l.py.  At a PythonLabs release mtg 
we agreed it was "a bug".  Offhand, though, I don't 
recall which part was considered buggy <wink/sigh>.

"""
From: Dale Strickland-Clark
Sent: Monday, October 22, 2001 6:20 PM
To: python-list@python.org
Subject: Augmented augmented assignment?

Can someone explain this?

It doesn't seem obvious to me but it's quite handy.

I assume it's something to do with the in-place 
operation of augmented assignment but I can't seem to 
reproduce it using normal assignment.

>>> x = 'spam'
>>> y = []
>>> y = y + x
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: can only concatenate list (not "string") to 
list
>>> y += x
>>> y
['s', 'p', 'a', 'm']
>>> 
"""

Note that both behaviors were in 2.0, so

a. This isn't due to "iterable object" generalization 
(it's due to list += mapping to list.extend).

and

b. There are backward-compatibility concerns.

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

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