[Python-ideas] Should this be considered a bug?
Nick Coghlan
ncoghlan at gmail.com
Sun Oct 30 23:31:18 CET 2011
On Mon, Oct 31, 2011 at 8:17 AM, Mike Meyer <mwm at mired.org> wrote:
> The only other iterable type you can add is tuples - and they don't
> display this behavior. So why not go ahead and fix this "bug" by
> making list.__add__ try converting the other object to a list, and
> adding a list.__radd__ with the same behavior?
>
> For that matter, a similar tweak for tuples wouldn't be inappropriate,
> and then the case of adding tuples & lists can raise a type error,
> paralleling the behavior of decimals and floats in the number
> hierarchy.
The universe of container types is a lot bigger than the Python
builtins. Suppose we made list.__add__ as permissive as list.__iadd__.
Further suppose we make collections.deque.__add__ similarly
permissive.
Now "[] + deque() == []", but "deque() + [] == deque()". Contrast that
with numeric types, which are designed to work together as a numeric
tower, such that the type of a binary operation's result does *not*
depend on the order of the operands: "1 + 1.1 == 2.1" and "1.1 + 1 ==
2.1".
Commutativity is an important expected property for addition and
multiplication. Even NumPy's arrays respect that by using those
operations for the element-wise equivalents - they use a separate
method for matrix multiplication (which is not commutative by
definition).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list