I really don’t understand where are __str__ and __repr__ used in Python. I mean, I get that __str__ returns the string representation of an object. But why would I need that? In what use case scenario? Also, I read about the usage of __repr__
But what I don’t understand is, where would I use them?
__repr____str__Use
__str__if you have a class, and you’ll want an informative/informal output, whenever you use this object as part of string. E.g. you can define__str__methods for Django models, which then gets rendered in the Django administration interface. Instead of something like<Model object>you’ll get like first and last name of a person, the name and date of an event, etc.__repr__and__str__are similar, in fact sometimes equal (Example fromBaseSetclass insets.pyfrom the standard library):