Skip to content

Instantly share code, notes, and snippets.

@shichao-an
Created February 4, 2016 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shichao-an/ec1b0ee1aa85d7b3c863 to your computer and use it in GitHub Desktop.
Save shichao-an/ec1b0ee1aa85d7b3c863 to your computer and use it in GitHub Desktop.
The Base class
class Base(object):
def __repr__(self):
try:
u = self.__str__()
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
repr_type = type(u)
return repr_type('<%s: %s>' % (self.__class__.__name__, u))
def __str__(self):
if hasattr(self, '__unicode__'):
if PY3:
return self.__unicode__()
else:
return unicode(self).encode('utf-8')
return txt_type('%s object' % self.__class__.__name__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment