问题
For Python list, is append() the same as +=?
I know that + will lead to the creation of a new list, while append() just append new stuff to the old list.
But will += be optimized to be more similar to append()? since they do the same thing.
回答1:
It's an __iadd__ operator. Docs.
Importantly, this means that it only tries to append. "For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y."
This thread specifically deals with lists and their iadd behavior
来源:https://stackoverflow.com/questions/53112952/python-list-append-vs