Why doesn't join() call str() on its arguments?

Leo Breebaart leo at lspace.org
Wed Feb 16 13:47:21 EST 2005


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...

-- 
Leo Breebaart  <leo at lspace.org>



More information about the Python-list mailing list