x += ... is not the same than x = x + ... if x is mutable
Colin J. Williams
cjw at ncf.ca
Thu Mar 21 13:35:26 CET 2013
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?
> 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.
More information about the Python-list