

Python: repr vs str dunder methods
source link: https://proinsias.github.io/til/python-repr-vs-str-dunder-methods/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

RealPython.com has a great article on how to think about the
__repr__()
and __str__()
methods for classes.
When should you use one method instead of the other?Permalink
In Short: Use
.__repr__()
for Programmers vs.__str__()
for Users.The reason there are two methods to display an object is that they have different purposes:
.__repr__()
provides the official string representation of an object, aimed at the programmer..__str__()
provides the informal string representation of an object, aimed at the user.In general, [
.__repr__()
] provides detailed and unambiguous information about the object. Another important property of the official string representation is that a programmer can normally use it to re-create an object equal to the original one.[The
.__str__()
] representation enables any user to understand the data contained in the object. Usually, it’s simpler and easier to read for a user.
As an example, consider an instance of the datetime
class in the datetime
module:
>>> import datetime
>>> today = datetime.datetime.now()
>>> today
datetime.datetime(2023, 2, 18, 18, 40, 2, 160890)
>>> print(today)
2023-02-18 18:40:02.160890
[The
.__repr__()
] representation shows the name of the data type and all the arguments needed to re-create the object. … [The.__str__()
representation] shows the [International Organization for Standardization (ISO)] standard format for displaying dates and times.
You can also access the official and informal string representations using the built-in functions
repr()
andstr()
:
>>> import datetime
>>> today = datetime.datetime.now()
>>> repr(today)
'datetime.datetime(2023, 2, 18, 18, 40, 2, 160890)'
>>> today.__repr__()
'datetime.datetime(2023, 2, 18, 18, 40, 2, 160890)'
>>> str(today)
'2023-02-18 18:40:02.160890'
>>> today.__str__()
'2023-02-18 18:40:02.160890'
How to use these methods with f-stringsPermalink
Note that:
f-strings display the informal string representation that
.__str__()
returns. You can get the official string representation from.__repr__()
by using the string conversion flag"!r"
in the f-string:
>>> f"{today}"
'2023-02-18 18:40:02.160890'
>>> f"{today!r}"
'datetime.datetime(2023, 2, 18, 18, 40, 2, 160890)'
You can also use f-strings with an equal sign (
=
) to show both the variable name and its value. You’ll use this option primarily for debugging purposes:
>>> f"{today = }"
'today = datetime.datetime(2023, 2, 18, 18, 40, 2, 160890)'
Note that when you use an equal sign, the f-string defaults to using the official string representation returned by
.__repr__()
. The representation aimed at the programmer is the most appropriate in this case since the equal sign in f-strings is normally used for debugging a program.You can override this behavior using the conversion flag
"!s"
:
>>> f"{today = !s}"
'today = 2023-02-18 18:40:02.160890'
An example class with these methodsPermalink
When you define a class, it’s a best practice to include the official string representation by defining
.__repr__()
. By including this method, you avoid the default representation, which isn’t very useful in most cases. This method will also provide a fallback option for the informal string representation, which comes in handy when you can use the same representation for both use cases.If you need to display the object to a user, then you can also define
.__str__()
. This method will provide an output that’s easier for the user to understand.
Here’s an example class:
>>> class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __repr__(self):
class_name = type(self).__name__
return f"{class_name}(title={self.title!r}, author={self.author!r})"
def __str__(self):
return f'"{self.title}" by {self.author}'
>>> odyssey = Book("The Odyssey", "Homer")
>>> print(repr(odyssey))
Book(title='The Odyssey', author='Homer')
>>> print(str(odyssey))
"The Odyssey" by Homer
Recommend
-
43
Magic methods are the methods that has two underscores as the prefix and suffix to the method name. These are also called dunder methods which is an adopted name for double underscores(methods with double underscores). __...
-
9
QQA: Python 中的 str 与 repr有时候,你会需要为你的类实现 __str__ 或 __repr__ 方法,你知道它们的作用是什么吗?它们有什么区别吗?这个问题的答案一搜就能找到,如果恰巧这是你第一次看到这个问题,不妨看...
-
11
Rust's SemVer Snares: repr(transparent) Super-Cut 2021-01-09 (Part of an ongoing series!) In the last two posts, repr(transpare...
-
8
为自己定义每个python类添加__repr__和__str__发布于 10 分钟前在类定义里,使用__str__和__repr__双下划线能够自行控制类中的字符串转换...
-
8
Copy link rfcbot commented
-
5
New issue Fix transmute_undefined_repr with single field #[repr(C)] structs #8425
-
7
Copy link Contributor Jarcho ...
-
4
When Should You Use .__repr__() vs .__str__() in Python? Remove ads One of the most common...
-
4
-
9
Conversation Member...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK