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.
__str__() and str()
If you have worked with other language before coming to Python, you might be familiar with str()
or string()
function, which would cast any other data type to String data type or class.
Python behaves the same. Here is an example in Python REPL.
>>> my_int = 2
>>> type(my_int)
<class 'int'>
>>> my_str = str(my_int)
>>> type(my_str)
<class 'str'>
Simlar is the repr()
function. repr
also returns a string.
>>> my_int = 2
>>> type(my_int)
<class 'int'>
>>> my_repr = repr(my_int)
>>> type(my_repr)
<class 'str'>
__repr__() and repr()
By now you understand that both the dunder method has their corresponding function symbols in which you can pass another data type.
We also saw in the previous section that when you pass anything to repr(), it simply turns into a str data type.
__str__() and __repr__() in a Class
If you still don't know about dunder methods, this section might help you undestand it.
There is a massive list of Python dunder methods. Dunder methods allow the way your handwritten class to behave in a certain manner.
The difference between the two methods is that __str__
is intended to provide a human-readable representation of the object, while __repr__
should provide a representation that can be used to recreate the object.
Let's see the difference between the two with the help of a simple code example:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
def __repr__(self):
return f"Point({self.x}, {self.y})"
p = Point(1, 2)
print(p) # Output: (1, 2)
print(repr(p)) # Output: Point(1, 2)
In the example above, __str__
returns a string representation of the Point object that is intended to be human-readable. The __repr__
method returns a string representation that could be used to recreate the object.
In general, it is recommended to always define a __repr__
method for your objects, so that you have an unambiguous representation of the object that can be used for debugging or in a REPL environment. On the other hand, __str__
is typically only defined when a more human-readable representation is needed. If __str__
is not defined for an object, repr(obj)
is used as a fallback.
It's worth noting that the __str__
method should return a string, while the __repr__
method should return a string that, when passed to the eval()
function, returns an object with the same value. The recommended format for __repr__
is a string that, when passed to eval()
, creates an object with the same value as the original. In the example above, the Point
object can be recreated by calling Point(1, 2)
.
Conclusion
In conclusion, __str__
and __repr__
are two important methods in Python that allow you to define string representations of objects. Understanding the difference between the two and when to use them is important in creating robust and well-designed code.
Keep reading:
Top comments (0)