Different behaviour for list.__iadd__ and list.__add__

__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 ‘o mutation magic.

Leave a Comment