"Leo Breebaart" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
I've tried Googling for this, but practically all discussions on
str.join() focus on the yuck-ugly-shouldn't-it-be-a-list-method?
issue, which is not my problem/question at all.

What I can't find an explanation for is why str.join() doesn't
automatically call str() on its arguments, so that e.g.
str.join([1,2,4,5]) would yield "1245", and ditto for e.g.
user-defined classes that have a __str__() defined.

All I've been able to find is a 1999 python-dev post by Tim
Peters which would seem to indicate he doesn't understand it
either:

   "string.join(seq) doesn't currently convert seq elements to
    string type, and in my vision it would. At least three of us
    admit to mapping str across seq anyway before calling
    string.join, and I think it would be a nice convenience
    [...]"

But now it's 2005, and both string.join() and str.join() still
explicitly expect a sequence of strings rather than a sequence of
stringifiable objects.

I'm not complaining as such -- sep.join(str(i) for i in seq) is
not *that* ugly, but what annoys me is that I don't understand
*why* this was never changed. Presumably there is some
counter-argument involved, some reason why people preferred the
existing semantics after all. But for the life of me I can't
think what that counter-argument might be...

I was originally going to say performance, but I don't think that's all that much of an issue.

For me, at least, I've already got a way of taking just about
anything and turning it into a string: the % operator. It's
powerful enough that there have been attempts to make
a less powerful and simpler to understand version.

The limitation here is that you have to know how many
elements you want to join, although even that isn't the
world's hardest issue. Consider:

(untested)
result = ("%s" * len(list)) % list

Not the most obvious code in the world, but it might
work.

And as someone else (you?) pointed out, this will
also work:

(untested)
result = "".join([str(x) for x in list])

and it's got the advantage that it will handle separators
properly.

John Roth


--
Leo Breebaart <[EMAIL PROTECTED]>

-- http://mail.python.org/mailman/listinfo/python-list

Reply via email to