In Python2, __repr__ (and __str__ ) should return a string object, not a unicode. In Python3, the situation reverses, __repr__ and __str__ should return unicode objects, not née string objects:
class Foo(object): def __repr__(self): return u'\N{WHITE SMILING FACE}' class Bar(object): def __repr__(self): return u'\N{WHITE SMILING FACE}'.encode('utf8') repr(Bar())
In Python2, you really have no choice. You must select an encoding for the return value of __repr__ .
By the way, did you read the PrintFails wiki ? It may not directly answer your other questions, but I found it useful in highlighting why some errors occur.
When using from __future__ import unicode_literals ,
'<{}>'.format(repr(x).decode('utf-8'))).encode('utf-8')
can simply be written as
str('<{}>').format(repr(x))
Assuming str is encoded in utf-8 on your system.
Without from __future__ import unicode_literals expression can be written as:
'<{}>'.format(repr(x))
unutbu Sep 02 '10 at 14:01 2010-09-02 14:01
source share