String Conversion in Python: When to Use __repr__ vs __str__

preview_player
Показать описание

When you define a custom class in Python and then try to print one of its instances to the console (or inspect it in an interpreter session) you get a relatively unsatisfying result. The default "to string" conversion behavior is basic and lacking in detail.

By default all you get is a string containing the class name and the `id` of the object instance (which is the object's memory address in CPython). That's better than *nothing*, but it's also not very useful.

The solution here is adding the `__str__` and `__repr__` "dunder" methods (some call them "magic methods") to your class.

They are the Pythonic way to control how objects are converted to strings in different situations.

In this tutorial video I'll do a deep dive on how Python's to-string conversion works and how you can add it to your own custom classes.

I'll walk you through the __str__ and __repr__ methods, when to use each, and some tips on how to use them in real world scenarios.

Just remember:

* The result of `__str__` should be readable.

* The result of `__repr__` should be unambiguous.

* Always add a `__repr__` to your classes. The default implementation for `__str__` just calls `__repr__`, so you get the best of both worlds.

* * *

FREE Python Tutorials & News:
Рекомендации по теме
Комментарии
Автор

so far most clear & best tutorial about this topic on yt. You have made a great video with amazing effort.

Rudra-gous
Автор

Great tutorials overrall. 10/10.

By the way, I just wanted to make a note. This probably came afterwards, considering how old the video is.

A simple solution is: __repr__ = __str__.

However, I'd be interested to read any feedback on this solution. Thank you

BijouBakson
Автор

Amazing content, first of all very informative, second your voice and delivery are amazing. Thank you for this video.

alexpeak
Автор

Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

akira_asahi
Автор

I like '...the Pythonic way to do this...' xD

matiturock
Автор

Great video! Thanks for your effort on making these videos!!!

jingyuchang
Автор

Could you Please explain
self = self

kishorekumar
Автор

This explanation is better than I found in paid course.

TechMarketer
Автор

thank your for your efforts. Could you tell me the name of the package, which shows method details as you write?

colonel
Автор

Hi there, great video! What do you mean by inspecting the car object? 3:52

KevinJohnson-wkbx
Автор

Great insight I had been struggling to comprehend this subject for quite some time, this video helped a lot

nazrulhassan
Автор

Thanks for insightful video. Got it checked.

iammayurn
Автор

I understand what you mean in the different use cases for both methods, but may I ask, why is it like that?. Why did the communitty agree that str is better for readability and repr for 'debugging'

diegovegacruz
Автор

Great job, I have just a small question: what should I do if i want to get car("red", "37281"), the diffrence is the quotation. Thank

anas.k
Автор

pretty explanatory and useful, thanks for this tutorial

carlosdelamorenacoco
Автор

what program are you using for python?

gabrielamos
Автор

Your python terminal looks cool with the auto-complete, etc. Are you using some third party software for that?

SaurabhAriyan
Автор

Great video(s). But you should have quotes around the {self.color}, no? So that repr(my_car) outputs: Car('red', 37281)

ropro
Автор

Hi! What font are you using in this video?

danebaird
Автор

this video helps me a lot 😍💚 thanks a lot man

kunalsoni
join shbcf.ru