Performance of incremental assignment is better

+=
-=
*=

These symbols are called incremental assignment operators .

a += bAnd a = a + bit 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 += bThink of it as a.extend(b)expanding on the original list.
a = a + bFirst, 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.

Guess you like

Origin blog.csdn.net/qq_46620129/article/details/112851537