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

From Nobody <nobody@nowhere.com>
Subject Re: x += ... is not the same than x = x + ... if x is mutable
Date 2013-03-21 04:27 +0000
Message-Id <pan.2013.03.21.04.27.38.40000@nowhere.com>
Newsgroups comp.lang.python
References <b5bfb026-fea4-4aec-8185-cc8837e1559a@googlegroups.com>
Organization Zen Internet

Show all headers | 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