Skip to content

Instantly share code, notes, and snippets.

@chejazi
Last active August 29, 2015 14:13
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 chejazi/c37a8c00485750fadf28 to your computer and use it in GitHub Desktop.
Save chejazi/c37a8c00485750fadf28 to your computer and use it in GitHub Desktop.
Test the priority of __str__ vs __repr__ in string coercion
class Foo(object):
def __init__(self):
noop = True
def __repr__(self):
return "using __repr__"
def __str__(self):
return "using __str__"
class Bar(object):
def __init__(self):
noop = True
def __str__(self):
return "using __str__"
class Baz(Bar):
def __init__(self):
super(Baz, self).__init__()
def __repr__(self):
return "using __repr__"
class Bad(object):
def __init__(self):
noop = True
def __repr__(self):
return "using __repr__"
foo = Foo()
bar = Bar()
baz = Baz()
bad = Bad()
print "Foo: " + str(foo)
print "Bar: " + str(bar)
print "Baz: " + str(baz)
print "Bad: " + str(bad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment