Skip to content

Instantly share code, notes, and snippets.

@benauthor
Created May 10, 2018 19:07
Show Gist options
  • Save benauthor/9184195f9b1f0b51ab7e98dad5b5b3c0 to your computer and use it in GitHub Desktop.
Save benauthor/9184195f9b1f0b51ab7e98dad5b5b3c0 to your computer and use it in GitHub Desktop.
python unicode-safe __repr__ pattern
# 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