Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41630

Re: x += ... is not the same than x = x + ... if x is mutable

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!reader01.nrc01.news.zen.net.uk.POSTED!not-for-mail
From Nobody <nobody@nowhere.com>
Subject Re: x += ... is not the same than x = x + ... if x is mutable
Date Thu, 21 Mar 2013 04:27:38 +0000
User-Agent Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-Id <pan.2013.03.21.04.27.38.40000@nowhere.com>
Newsgroups comp.lang.python
References <b5bfb026-fea4-4aec-8185-cc8837e1559a@googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Lines 13
Organization Zen Internet
NNTP-Posting-Host 348a5a8d.news.zen.co.uk
X-Trace DXC=ONWnCb3;KM0\c0=1\i[2^3a0UP_O8AJo<=dR0\ckLKG0WeZ<[7LZNR68EblaVK`U]5M2Z^cWRFGA;oR1`;=bfb;6
X-Complaints-To abuse@zen.co.uk
Xref csiph.com comp.lang.python:41630

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web