String Conversion for Python Containers (Lists, Dicts, …)
This is the final lesson in the course. In it, you’ll see how containers like lists and tuples convert their children to strings when called with str()
. The lesson also encourages you to add a .__repr__()
method to your classes.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Okay, so before we wrap this up, there’s one more thing I wanted to mention, and that is how containers convert their child objects—the objects they contain—to string.
00:12
Maybe the surprising thing is that even if you call str()
on a container—so, I’m creating this list object here—even if you call str()
on a container, it’s going to represent the internal objects with the .__repr__()
function.
00:28
So when I—you know, I’m using the today
object, which is just a datetime
object—or, datetime.date
object. So, when I have this list here of these three date
objects and then I call str()
on it, I actually get a string back that has the container with the .__repr__()
inside.
00:45
So, this is just something to keep in mind how that works. If you wanted to convert those with the str()
function individually, you would just need a loop or some kind of list comprehension to do that manually. But you know, that’s just a side note.
00:58
I think, really, the bottom line or the one takeaway from this thing here is # At least add a __repr__ to your classes :)
. All right! So, I hope you learned something new today and you’re going to be able to apply that in a real-world program or in some code that you write. Cool! Well, happy Pythoning, and I’ll talk to you soon.
paulakula11 on Aug. 28, 2019
concise and clear
Silver on Dec. 7, 2019
awesome!
Lokman on Feb. 27, 2020
Thanks @DanBader for best practice always provide __repr__
for classes. I guess dunder repr short form for represent.
sroux53 on June 5, 2020
Excellent!
Vaibhav Kumar on July 19, 2020
Very well crisp and concise.
avalidzy on July 23, 2020
Excellent voice delivery! The “dunder” use methodology is more apparent to me. Thanks Dan!
Idris Diba on Aug. 3, 2020
I learned a lot from this tutorial. Thank you Dan for your excellent method of teaching.
vikrant06 on Aug. 16, 2020
neat
Ghani on Oct. 7, 2020
Very good though very short!
cap-gnc on Oct. 9, 2020
Thank you. __str__
vs __repr__
demystified.
geultrin on Nov. 5, 2020
Thanks Dan. Short and sweet. I knew about dunder repr but did not understand it fully. I now see its usefulness
loopology on Nov. 29, 2020
This tutorial is from 2017 and it shows: It’s using “{}”.format() instead of the f”{}” string formatting.
What I miss about all RealPython tutorials: They should state their publication date up front as well as the Python version being used, and possibly what would need to be changed to be up to date.
Abderraouf Z on Nov. 30, 2020
Top explanations!
nathank on Dec. 12, 2020
I thought I knew repr very well but this just adds an additional level of depth. Thanks Dan!
dbristow31 on Jan. 28, 2021
This has been helpful. I made a note to myself to include a repr method to all of my classes, moving forward.
jeffersongarciaor on Jan. 29, 2021
Just amazing to learn new stuffs. Thank you Dan for this tutorial.
shangcode on Feb. 20, 2021
Learned a lot! 😀
Cavalcante on March 18, 2021
That’s Gold! I have seen this use of __ str__ in django when you use the admin interface, but I did not know it worked undercover.
Thanks for the lesson.
amirrastkhadiv on April 7, 2021
That’s informative. Thanks Dan!
Espius Edwards on Sept. 3, 2021
Thank you for your concise tutorial!
François 江戸 D on Sept. 21, 2021
No sooner said than used, my last piece of code got cleaner :)
MakerMike369 on March 7, 2022
Was looking for info for making a “Class template” of sorts. .__repr__()
fits the bill nicely!
mckown on June 30, 2022
Excellent presentation! I learned quite a bit. Thanks!
Become a Member to join the conversation.
michelnakhla on July 20, 2019
Very brief and nice.