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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 21 13:39:04 CET 2013


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



More information about the Python-list mailing list