Python distinguishes between the + and += operators and provides separate bindings for this; __add__ and __iadd__ . The list() just provides a different implementation for the latter.
For lists, it is more efficient to implement them separately; __add__ should return a whole new list, and __iadd__ could just extend self and then return self .
In C code, __iadd__ implements list_inplace_concat() , which simply calls listextend() or, in python code, [].extend() . The latter takes any sequence, by design.
On the other hand, the __add__ method, introduced in C by list_concat , accepts only list , possibly for efficiency; it can loop directly on the internal C array and copy elements to a new list.
In conclusion, the reason __iadd__ accepts any sequence, because when PEP 203 (the added add clause) was implemented, for lists it was easiest to reuse the .extend() method.
Martijn pieters
source share