[Python-Dev] join() et al.

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 16 May 2000 13:15:40 -0500 (CDT)


    Marc> Ok, here's a more readable and semantically useful definition:
    ...

    >>> join((1,2,3))
    6

My point was that the verb "join" doesn't connote "sum".  The idea of
"join"ing a sequence suggests (to me) that the individual sequence elements
are still identifiable in the result, so "join((1,2,3))" would look
something like "123" or "1 2 3" or "10203", not "6".

It's not a huge deal to me, but I think it mildly violates the principle of
least surprise when you try to apply it to sequences of non-strings.

To extend this into the absurd, what should the following code display?

    class Spam: pass

    eggs = Spam()
    bacon = Spam()
    toast = Spam()

    print join((eggs,bacon,toast))

If a join builtin is supposed to be applicable to all types, we need to
decide what the semantics are going to be for all types.  Maybe all that
needs to happen is that you stringify any non-string elements before
applying the + operator (just one possibility among many, not necessarily
one I recommend).  If you want to limit join's inputs to (or only make it
semantically meaningful for) sequences of strings, then it should probably
not be a builtin, no matter how visually annoying you find

    " ".join(["a","b","c"])

Skip