Created
February 4, 2016 06:35
-
-
Save shichao-an/ec1b0ee1aa85d7b3c863 to your computer and use it in GitHub Desktop.
The Base class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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