I am making a class that records a result from an named query interaction with the database. I am trying to make it print nicely so I can log it and it looks nice. My code is
class Response(object):
# TODO Figure out if these should remain keywords or shold just be positional argsProbably should not be keywords b
def __init__(self, success=False, result=None, error=None, errorType=None):
self.success = success
self.result = result
self.error = error
self.errorType = errorType
def __str__(self):
if self.success:
return "Success: True, Result: %s"%(str(self.result))
else:
return "Error Type %s Error Text: %s"%(self.errorType, self.error)
def __repr__(self):
if self.success:
return "Success: True, Result: %s"%(str(self.result))
else:
return "Error Type %s Error Text: %s"%(self.errorType, self.error)
However, doing
r = forms2.common.Response(success=True, result='Hi!', error=None, errorType=None)
print str(r)
prints <common.Response object at 0x3>
. Not sure what I am missing here. When __str__
was not doing the trick I though maybe I needed __repr__
but I thought __repr__
is more for printing out the configuration of the class/all the attributes that it was initialized too for recreation purposes. What am I doing wrong?