Different behaviour for list.__iadd__ and list.__add__
The first mutates the list, and the second rebinds the name.
__iadd__
mutates the list, whereas __add__
returns a new list, as demonstrated.
An expression of x += y
first tries to call __iadd__
and, failing that, calls __add__
followed an assignment (see Sven's comment for a minor correction). Since list
has __iadd__
then it does this little bit of mutation magic.