Short confusing example with unicode, print, and __str__

Peter Otten __peter__ at web.de
Thu Mar 6 09:13:52 CET 2008


Gerard Brunick wrote:

> It seems the question is more about what does print do.  Lets extend
> your example:
> 
> >>> d=unicode("Caf\xe9", "Latin-1")
> >>> repr(d)
> "u'Caf\\xe9'"
> >>> print d
> Café
> >>> str(d)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
> position 3: ordinal not in range(128)
> 
> Why doesn't the print statement that a UnicodeEncodeError?  I assumed
> that print calls str and then prints
> the result, but this doesn't seem to be the case.  What the heck does
> print do?

Something like

d = ...
if type(d) is unicode:
    sys.stdout.write(d.encode(sys.stdout.encoding))
else:
    sys.stdout.write(str(d))

Unfortunately you can't make that work smoothly with arbitrary objects as
you have to throw in an explicit conversion to unicode:

>>> class C(object):
...     def __unicode__(self): return u"Caf\xe9"
...
>>> print C()
<__main__.C object at 0x2b1da33e0bd0>
>>> print unicode(C())
Café

Or, even less intuitive:

>>> class D(object):
...     def __str__(self): return u"Caf\xe9"
...
>>> print unicode(D())
Café
>>> print D()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position
3: ordinal not in range(128)

Peter



More information about the Python-list mailing list