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

John Machin sjmachin at lexicon.net
Wed Feb 16 14:54:43 EST 2005


On 16 Feb 2005 18:47:21 GMT, Leo Breebaart <leo at lspace.org> wrote:


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

For a start, I think you meant ''.join([1,2,4,5]) to yield "1245".

Secondly, concatenating arbitrary types with a null separator doesn't
appear to be a good use case.
E.g.
>>> ''.join([str(x) for x in [1,2,1./3,4]])
'120.3333333333334'

Some possible explanations:

1. Explicit is better than implicit.
2. It would only be a good "trick" IMHO with a non-null separator and
types with a 'clean' str() result (unlike float) -- like int; I can't
think of more at the moment.
3. It would be one step on the slippery downwards path to perlishness.
4. For consistency, would you like "1" + 2 to produce "12"?





More information about the Python-list mailing list