Short confusing example with unicode, print, and __str__

Gerard Brunick gbrunick at andrew.cmu.edu
Thu Mar 6 08:44:54 CET 2008


Gary Herron wrote:
> Gerard Brunick wrote:
>> I really don't understand the following behavior:
>>
>>  >>> class C(object):
>> ...     def __init__(self, s): self.s = s
>> ...     def __str__(self): return self.s
>> ...
>>  >>> cafe = unicode("Caf\xe9", "Latin-1")
>>  >>> c = C(cafe)
>>  >>> print "Print using c.s:", c.s
>> Print using c.s: Café
>>  >>> print "Print using just c:", c
>> Print using just c: 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)
>>  >>> str(c)
>> 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 would "print c.s" work but the other two cases throw an exception?
>> Any help understanding this would be greatly appreciated.
>>
>> Thanks in advance,
>> Gerard
>>   
> It's the difference between how __str__ and __repr__  act on strings.
>
> Here's s simpler example
>
> >>> d=unicode("Caf\xe9", "Latin-1")
> >>> repr(d)
> "u'Caf\\xe9'"
> >>> 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)
>
> Gary Herron
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?



More information about the Python-list mailing list