Created
May 10, 2018 19:07
-
-
Save benauthor/9184195f9b1f0b51ab7e98dad5b5b3c0 to your computer and use it in GitHub Desktop.
python unicode-safe __repr__ pattern
This file contains hidden or 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
# This is a pattern I like but I always forget the details | |
class Boo(object): | |
def __init__(self): | |
self.foo = u"☃" | |
def __unicode__(self): | |
return u"Boo(foo={})".format(self.foo) | |
def __str__(self): | |
return self.__unicode__().encode('utf-8') | |
__repr__ = __str__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment