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

Steven Bethard steven.bethard at gmail.com
Wed Feb 16 14:49:53 EST 2005


Leo Breebaart wrote:
> 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.

py> chars = [u'ä', u'å']
py> ', '.join(chars)
u'\xe4, \xe5'
py> ', '.join(str(c) for c in chars)
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 1, in <generator expression>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in 
position 0: ordinal not in range(128)
py> u', '.join(chars)
u'\xe4, \xe5'
py> u', '.join(unicode(c) for c in chars)
u'\xe4, \xe5'

Currently, str.join will return a unicode object if any of the items to 
be joined are unicode.  That means that str.join accepts unicode objects 
as well as str objects.  So you couldn't just call str on all the objects...

Maybe you could call str or unicode on each object as appropriate 
though...  If str.join already determines that it must return a unicode 
object, it could call unicode on all the items instead of str...  I 
don't know the code well enough though to know if this is feasible...

STeVe



More information about the Python-list mailing list