+=
-=
*=
These symbols are called incremental assignment operators .
a += b
And a = a + b
it is equivalent in the results, but a better performance of the former.
+=
The magic method used is __iadd__
.
Take a list as an analogy:
a += b
Think of it as a.extend(b)
expanding on the original list.
a = a + b
First, take out the value from the original list and put it into a new list for expansion.
Obviously, the latter is more expensive.
Therefore, try to use incremental assignment in use.