Best output type and encoding practices for __repr__() functions?
In Python2, __repr__ (and __str__) must return a string object, not a unicode object. In Python3, the situation is reversed, __repr__ and __str__ must return unicode objects, not byte (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()) # ☺ repr(Foo()) # UnicodeEncodeError: ‘ascii’ …