I have read and understood the difference between repr and str and noted the difference.
repr
- unambigousstr
- readable
What happens is both is used? I wrote a simple class and found that repr is called when both are given.
class test():
def __repr__(self):
return 'repr called'
def __str__(self):
return 'str called'
x = test()
print x
Now, when I check the implementation of both the classes in Django ValidationError, Im not sure when str will be called as the repr function is already overloaded.
https://docs.djangoproject.com/en/2.0/_modules/django/core/exceptions/#ValidationError
def __str__(self):
# print 'validationerror str'
if hasattr(self, 'error_dict'):
return repr(dict(self))
return repr(list(self))
def __repr__(self):
# print 'validationerror repr'
return 'ValidationError(%s)' % self
How and when the __str__
function will be called?