UnicodeEncodeError during repr()

Dave Angel davea at ieee.org
Mon Apr 19 22:41:25 EDT 2010


gb345 wrote:
> In <hqguja$tt$1 at online.de> "Martin v. Loewis" <martin at v.loewis.de> writes:
>
>   
>>> Do I need to do something especial to get repr to work strictly
>>> with unicode?
>>>       
>
>   
>> Yes, you need to switch to Python 3 :-)
>>     
>
>   
>>> Or should __repr__ *always* return bytes rather than unicode?
>>>       
>
>   
>> In Python 2.x: yes.
>>     
>
>   
>>> What about __str__ ?
>>>       
>
>   
>> Likewise.
>>     
>
>   
>>> If both of these are supposed to return bytes,
>>> then what method should I use to define the unicode representation
>>> for instances of a class?
>>>       
>
>   
>> __unicode__.
>>     
>
> Thanks!
>
>
>   
 More precisely, __str__() and __repr__() return characters.  Those 
characters are 8 bits on Python 2.x, and Unicode on 3.x.   If you need 
unicode on 2.x, use __unicode__().

DaveA



More information about the Python-list mailing list