[Python-ideas] Should this be considered a bug?

Mike Meyer mwm at mired.org
Sun Oct 30 23:17:49 CET 2011


On Sun, 30 Oct 2011 20:55:29 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> On Sun, Oct 30, 2011 at 8:29 PM, Mike Meyer <mwm at mired.org> wrote:
> > h = [1, 2, 3]
> > d = dict(a=1, b=2)
> > h += d # works
> > h = h + d # exception
> No, the reason the latter fails is because it's unclear what the type
> of the result should be and the interpreter refuses to guess. In the
> former case, there's no such ambiguity (since the type of 'h' simply
> stays the same).

While that's perfectly reasonable, it implies that some result type
other than list is possible. I can't see what that would be since you can't add dictionaries.

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.

    <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list