x += ... is not the same than x = x + ... if x is mutable
Nobody
nobody at nowhere.com
Thu Mar 21 00:27:38 EDT 2013
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.
More information about the Python-list
mailing list