Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41593 > unrolled thread
| Started by | bartolome.sintes@gmail.com |
|---|---|
| First post | 2013-03-20 07:17 -0700 |
| Last post | 2013-03-21 12:39 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
x += ... is not the same than x = x + ... if x is mutable bartolome.sintes@gmail.com - 2013-03-20 07:17 -0700
Re: x += ... is not the same than x = x + ... if x is mutable Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-20 16:26 +0200
Re: x += ... is not the same than x = x + ... if x is mutable Nobody <nobody@nowhere.com> - 2013-03-21 04:27 +0000
Re: x += ... is not the same than x = x + ... if x is mutable "Colin J. Williams" <cjw@ncf.ca> - 2013-03-21 08:35 -0400
Re: x += ... is not the same than x = x + ... if x is mutable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-21 12:39 +0000
| From | bartolome.sintes@gmail.com |
|---|---|
| Date | 2013-03-20 07:17 -0700 |
| Subject | x += ... is not the same than x = x + ... if x is mutable |
| Message-ID | <b5bfb026-fea4-4aec-8185-cc8837e1559a@googlegroups.com> |
Hi, I thought that x += ... was the same than x = x + ..., but today I have realized it is not true when operating with mutable objects. In Python 3.3 or 2.7 IDLE (Windows) compare: >>> a = [3] >>> b = a >>> a = a + [1] >>> b [3] and >>> a = [3] >>> b = a >>> a += [1] >>> b [3, 1] Is this behaviour explained in the Python documentation? Thanking you in advance, Bartolomé Sintes
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-03-20 16:26 +0200 |
| Message-ID | <qot38vqw3jn.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #41593 |
bartolome.sintes@gmail.com writes: > Hi, > > I thought that x += ... was the same than x = x + ..., but today I > have realized it is not true when operating with mutable objects. > > In Python 3.3 or 2.7 IDLE (Windows) compare: > >>> a = [3] > >>> b = a > >>> a = a + [1] > >>> b > [3] > > and > >>> a = [3] > >>> b = a > >>> a += [1] > >>> b > [3, 1] > > Is this behaviour explained in the Python documentation? Yes, it's documented in the language reference, specifically in the latter half of the paragraph quoted below. <http://docs.python.org/3/reference/simple_stmts.html#assignment-statements> # An augmented assignment expression like x += 1 can be rewritten as x # = x + 1 to achieve a similar, but not exactly equal effect. In the # augmented version, x is only evaluated once. Also, when possible, # the actual operation is performed in-place, meaning that rather than # creating a new object and assigning that to the target, the old # object is modified instead.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2013-03-21 04:27 +0000 |
| Message-ID | <pan.2013.03.21.04.27.38.40000@nowhere.com> |
| In reply to | #41593 |
On Wed, 20 Mar 2013 07:17:08 -0700, bartolome.sintes wrote: > I thought that x += ... was the same than x = x + ..., but today I have > realized it is not true when operating with mutable objects. It may or may not be the same. x += y will invoke x.__iadd__(y) if x has an __iadd__ method, otherwise x + y will be evaluated and the result assigned to x. In the first case, x will always continue to refer to the same object (i.e. id(x) won't change). In the second case, x will typically (but not always) refer to a different object.
[toc] | [prev] | [next] | [standalone]
| From | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| Date | 2013-03-21 08:35 -0400 |
| Message-ID | <kieuqi$29m$1@theodyn.ncf.ca> |
| In reply to | #41630 |
On 21/03/2013 12:27 AM, Nobody wrote: > On Wed, 20 Mar 2013 07:17:08 -0700, bartolome.sintes wrote: > >> I thought that x += ... was the same than x = x + ..., but today I have >> realized it is not true when operating with mutable objects. > > It may or may not be the same. x += y will invoke x.__iadd__(y) if x has > an __iadd__ method, otherwise x + y will be evaluated and the result > assigned to x. > Does this depend on whether Py27 or Py32 is used? Colin W. > In the first case, x will always continue to refer to the same object > (i.e. id(x) won't change). In the second case, x will typically (but not > always) refer to a different object. >
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-21 12:39 +0000 |
| Message-ID | <514aff68$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41646 |
On Thu, 21 Mar 2013 08:35:26 -0400, Colin J. Williams wrote: > On 21/03/2013 12:27 AM, Nobody wrote: >> On Wed, 20 Mar 2013 07:17:08 -0700, bartolome.sintes wrote: >> >>> I thought that x += ... was the same than x = x + ..., but today I >>> have realized it is not true when operating with mutable objects. >> >> It may or may not be the same. x += y will invoke x.__iadd__(y) if x >> has an __iadd__ method, otherwise x + y will be evaluated and the >> result assigned to x. >> > Does this depend on whether Py27 or Py32 is used? No. It is equally true for all versions of Python. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web