On Sun, Oct 30, 2011 at 9:10 PM, Georg Brandl
It's still a bit inconsistent though (e.g. it doesn't work with tuple's __iadd__).
Tuple doesn't have an __iadd__, it's immutable :) As Antoine noted, list.__iadd__ is actually just list.extend under the hood, and accepts an arbitrary iterable. We're a bit more restrictive with other mutable container types (e.g. set, bytearray), but in those cases, the corresponding methods also only accept a limited selection of object types. (There's also a longstanding limitation in the type coercion system where returning NotImplemented doesn't work properly for the sequence slots at the C level - you have to use the numeric slots instead. That issue should finally be resolved in 3.3)
Also, "type stays the same" is not true in other instances, e.g.
i = 1 i += 0.5
Again, that's an immutable type, and the various numeric types go to great lengths in order to play nicely together and negotiate the "correct" result type. Even there, we deliberately complain when we think something dodgy might be going on:
import decimal decimal.Decimal(1) + 1.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia