printing non-ASCII characters - please explain
aleax at aleax.it
Sun Jun 15 15:54:21 CEST 2003
Gerhard Häring wrote:
> Helmut Jarausch wrote:
>> please explain the following difference to me (with cvs-python)
>> print N
>> this prints Höfig which contains the Germain umlaut 'ö' = oe = \xf6
>> print [N]
> Because print invokes repr() on the object to print.
str(), actually -- that's the reason the umlaut shows when you
just print N.
> repr for lists is implemented in C, but if it were written in Python, it
> would probably look like:
> class list:
> def __repr__(self):
> return "[%s]" % ", ".join([repr(x) for x in self.items])
Yes, and __str__ is much the same, which is where the problem comes in.
A list's __str__ calls repr(), NOT str(), on the items (because it's
felt that using an item's str() might be confusing if that item was a
string including commas, basically).
>> - and how can I change this?
> Use an explicit function to format the list how you want it to be. If
> you're not aware of the difference between str() and repr(), I'd
> recommend you look them up in the Python documentation.
Yes, this advice is indeed the only way to go.
More information about the Python-list