A trivial question about print
Chris Liechti
cliechti at gmx.net
Thu Apr 11 15:36:07 EDT 2002
Alex Martelli <aleax at aleax.it> wrote in
news:kTft8.6500$b62.159068 at news1.tin.it:
> Max M wrote:
> ...
>> def newJoin(self, items):
>> return self.join([str(i) for i in items])
>>
>> Is there any case where it isn't applicable at all?
>
> Could be a disaster if any item was Unicode:
>
>>>> print ''.join(('mapp','erch',u'\u00e8')).encode('latin-1')
> mapperchè
>
> vs:
>
>>>> print ''.join([str(x) for x in
> ('mapp','erch',u'\u00e8')]).encode('latin-1')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> UnicodeError: ASCII encoding error: ordinal not in range(128)
>>>>
>
> but presumably you could have something subtler that used
> str() or unicode() as appropriate (maybe not trivial to
> determine what's "appropriate" in each given case).
how about a formater argument?
>>> class mystr(str):
... def join(self, elements, formater = None):
... if formater is not None:
... elements = map(formater, elements)
... return str.join(self,elements)
...
>>> mystr(',').join(range(10),str)
'0,1,2,3,4,5,6,7,8,9'
so the user can use 'unicode', 'str' or 'curry(unicode, encoding="latin1")'
etc.
chris
PS: it would be realy cool if curry was a builtin :-)
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list