Why doesn't join() call str() on its arguments?
Jeff Shannon
jeff at ccvcorp.com
Wed Feb 16 20:38:15 EST 2005
Roy Smith 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.
>>
>>That would be the wrong thing to do when the arguments are unicodes.
>
> Why would it be wrong? I ask this with honest naivete, being quite
> ignorant of unicode issues.
As someone else demonstrated earlier...
>>> str(u'ü')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>>
Using str() on a unicode object works... IF all of the unicode
characters are also in the ASCII charset. But if you're using
non-ASCII unicode characters (and there's no point to using Unicode
unless you are, or might be), then str() will throw an exception.
The Effbot mentioned a join() implementation that would be smart
enough to do the right thing in this case, but it's not as simple as
just implicitly calling str().
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list