
__str__ vs __repr__ in Python and When to Use Them
Introduction # In a previous post, we have done a comparison between __new__ and __init__. In this post, we are going to do something similar, but with a different pair.
For those who don’t know, __str__() and __repr__() are two dunder methods which you can override in a class to change the way class is represented.
These two little functions have been a source of confusion in my early career. Let us first go through them one by one and let us understand what they do first. Then we’ll do comparison among them with respect to their similarities and differences.