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

news.sydney.pipenetworks.com nytimes at swiftdsl.com.au
Thu Feb 17 22:05:04 EST 2005


Duncan Booth wrote:
> 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.

I see your point but I'm not totally convinced  I don't understand 
unicode that well so I'll just be quiet now.

Your point about int and long vs str and unicode is interesting though. 
Does it mean str and unicode will some time in the future be unified 
once all the differences are sorted out ?

Regards,

Huy



More information about the Python-list mailing list