Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Responses From Literals

00:00 In the previous lesson, I introduced you to __str__ and __repr__. In this lesson, I’ll show you some cases where the response from those two methods are the same.

00:10 For some Python objects. __str__ and __repr__ return the same values. Most literals in Python, which of course are implemented as objects, have this behavior.

00:21 Let’s go to the REPL and see it in action. A literal is a fixed value of a specific data type. For example, a string is a literal, as are integers. One is the loneliest number.

00:36 In fact, in Python, it literally is so. Yeah, bad dad joke. Printing 1 converts it to a string, resulting in the same value as output, which you’ll recall from the previous lesson, means that when I invoke the str() callable, I get a string version.

00:55 How about the repr() version? And if you’ve been paying attention, that shouldn’t be a surprise. That’s what I meant when I said the str() and repr() versions are the same for literals.

01:05 Let’s look at some others.

01:10 Here, I have a string. That’s it printed, and that’s it converted, which isn’t really much of a conversion because it was already a string. And then when I evaluate it, I get the repr() version, which is quite literally the same.

01:28 Alright, I’ll stop now. How about for lists?

01:34 Here are some numbers evaluated as a repr(), printed, converted to a string, and explicitly converted to the repr(). Let’s try a dictionary.

01:55 person printed,

02:00 str() and repr(). Just when you think it might be predictable, there’s a tiny little corner case. You can also use curly brackets to denote a set.

02:17 My set of fruit, and because it’s a set, the apple is only there once. So far, the repr() version is what you might expect. I’ll convert it to a string

02:29 and explicitly to the repr(). And now for that corner case I was talking about,

02:38 what type do you think dogs is? Empty curly brackets don’t really help. Let’s use type() to find out. Right. Curly brackets are for both dictionaries and sets.

02:52 I’ve actually never liked this about Python. I would’ve been perfectly happy for a set to require a class instance and not have the curly bracket shortcut. Sets get used so much less frequently than dicts that I find this syntax confusing.

03:06 It almost always takes me a minute I often have the moment of “Wait, where are the keys in this dictionary?” Maybe this doesn’t throw you the way it throws me, but either way, it is how it works in Python.

03:18 So if empty braces are dictionaries, how do you get an empty set? Well, by using the class. cats is empty. And to distinguish it from a dictionary, the repr() shows the class name.

03:34 So you can use the brace shortcut, but only if your set has stuff inside of it. This brings me back to my complaint because if I then put something in the set, the repr() value will change to the brace bracket format.

03:47 It just feels inconsistent to me. Enough moaning. For completionist’s sake, you can see that the type of cats and the type of dogs are different things.

04:00 Next up, an in-depth look at how multi-dimensionality affects the theory of quantum gravity.

Become a Member to join the conversation.