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?
The official Python documentation says __repr__() is used to compute the “official” string representation of an object. The repr() built-in function uses __repr__() to display the object. __repr__() returns a printable representation of the object, one of the ways possible to create this object.
str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.
The difference between str() and repr() is: The str() function returns a user-friendly description of an object. The repr() method returns a developer-friendly string representation of an object.
__repr__
Called by the
repr()
built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).
__str__
Called by the
str()
built-in function and by the print statement to compute the "informal" string representation of an object.
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 from BaseSet
class in sets.py
from the standard library):
def __repr__(self): """Return string representation of a set. This looks like 'Set([<list of elements>])'. """ return self._repr() # __str__ is the same as __repr__ __str__ = __repr__
The one place where you use them both a lot is in an interactive session. If you print an object then its __str__
method will get called, whereas if you just use an object by itself then its __repr__
is shown:
>>> from decimal import Decimal >>> a = Decimal(1.25) >>> print(a) 1.25 <---- this is from __str__ >>> a Decimal('1.25') <---- this is from __repr__
The __str__
is intended to be as human-readable as possible, whereas the __repr__
should aim to be something that could be used to recreate the object, although it often won't be exactly how it was created, as in this case.
It's also not unusual for both __str__
and __repr__
to return the same value (certainly for built-in types).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With