Steven D'Aprano wrote: > On Wed, 14 Feb 2007 19:45:14 -0800, James Stroud wrote: >>Steven D'Aprano wrote: >>>Since lists and tuples are completely different objects with completely >>>different usages, what should concatenating a list and a tuple give? >>>Should it depend on the order you pass them? >> >>Is that a guess or just common sense? > > Sorry, is *what* a guess?
That it should depend on order. > Conceptually, ints are a subset of floats (they certainly are in pure > mathematics). Automatic coercions from ints to floats makes sense; > automatic coercions the other way rarely do -- should you round up or > round down or truncate? What is right in one application is not right for > another. > > Lists and tuples, on the other hand, are conceptually two distinct data > types. Adding a list to a tuple is no more sensible than adding a list to > a string -- just because they're both sequences doesn't mean adding them > together is meaningful. >>Do you guess with __add__ and __radd__? > > No. If there is an obviously correct behaviour for addition (like with > ints and floats) then I coerce the objects appropriately. If there is no > obviously correct behaviour, I refuse to guess. > > The user's expected behaviour for [1] + (1,) might be to return a list, or > it might be to return a tuple. Since there is no obviously correct > behaviour, the right thing to do is to refuse to guess. I guess we differ on what is obvious. This seems obvious to me: [1] + (1,) => [1, 1] (1,) + [1] => (1, 1) simply becuase the operand on the left should take precendence because its "__add__" is called and its "__add__" returns a list. In essence, as we know the obviously correct behavior for __add__ and __radd__, then it would be the obviously correct behavior that the above would follow. I would venture to guess that most people would intuitively consider the above behavior correct, simply because the information content difference between a list versus a tuple is non-existent (outside of the information that one is a list and the other a tuple). Why would their types dictate coercion? With ints and floats, as you point out, the reasons that type dictates coercion are obvious and mathematical. Thus, for tuples and lists, it wouldn't make sense to consider one type taking precendence over the other, so we fall back to position, which seems to be the common sense approach. James -- http://mail.python.org/mailman/listinfo/python-list