Augmented assignment

Andrea Griffini agriff at tin.it
Tue Feb 21 02:28:31 EST 2006


I think it heavily depends on what is "x". If x is bound to a mutable
x=x+1 and x+=1 can not only have different speed but indeed can do two
very unrelate things (the former probably binding to a new object, the
latter probably modifying the same object). For example consider what
happens with lists and [1] instead of 1...

>>> s = []
>>> t = s
>>> t = t + [1]
>>> t
[1]
>>> s
[]
>>> s2 = []
>>> t2 = s2
>>> t2 += [1]
>>> t2
[1]
>>> s2
[1]
>>>

Also if x is not a single name but a more convoluted expression with +=
that expression is evaluated once and even in this case there can be
differences in speed and not only in speed.




More information about the Python-list mailing list