Lately, I\'ve had lots of trouble with __repr__()
, format()
, and encodings. Should the output of __repr__()
be encoded or be
I think a decorator can manage __repr__
incompatibilities in a sane way. Here's what i use:
from __future__ import unicode_literals, print_function
import sys
def force_encoded_string_output(func):
if sys.version_info.major < 3:
def _func(*args, **kwargs):
return func(*args, **kwargs).encode(sys.stdout.encoding or 'utf-8')
return _func
else:
return func
class MyDummyClass(object):
@force_encoded_string_output
def __repr__(self):
return 'My Dummy Class! \N{WHITE SMILING FACE}'