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

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 17 10:12:47 EST 2005


news.sydney.pipenetworks.com wrote:

> I'm not sure if this has been raised in the thread but I sure as heck 
> always convert my join arguments using str(). When does someone use 
> .join() and not want all arguments to be strings ? Any examples ?

This has already been raised, but maybe not in exactly this context. You 
don't want to convert your arguments to be strings if some of them are 
unicode.

If I do:

  res = str.join(', ', [a, b, c])

then res is of type str if, and only if, a, b, and c are of type str.
If any of a, b, or c are of type unicode, then res is unicode.

In effect, this means you don't have to worry about whether you are 
manipulating str or unicode at this point in your program (kind of 
comparable to not caring whether the integer you are using is int or long).

When you come to output the string you do need to care, as when it is 
unicode you may have to encode it, but at least the internal manipulations 
can ignore this possibility.

Of course you could just call unicode on everything but for simple 
applications you might not want to handle unicode at all. That's not a 
decision Python can make for you so it shouldn't guess.



More information about the Python-list mailing list