Using .__dict__ Explicitly in the Person Class
00:00
So now that you have a good understanding of the .__dict__
attribute, you’ll see how you can inspect and use this attribute in real life. And the example you’ll be working with is the Person
class in the person.py
file.
00:17
So here’s the class, and the intention is that you’ll instantiate this class. So if you start with an __init__()
method that takes three input parameters as you can see on line two, there’s a first name, a last name, and age.
00:31
And all the __init__()
method does is it creates the three instance attributes to then assign those input values to self.first_name
, self.last_name
, and self.age
.
00:46
Secondly, on line seven, there is a __str__()
dunder method. And all that dunder method does is it returns an f-string. And that f-string takes the values of those instance attributes.
00:58 Now, I’ve split this over three lines just for readability, just so that it fits on the screen, and you will see why that is important in a minute when you start changing this f-string.
01:10
But for now, if you could focus on the as_tuple()
method on line 12, this method returns a tuple as the name would suggest. But if you have a look at what is between the parentheses on line 13, you will see the first use of the .__dict__
attribute.
01:27
So what is going on here? Well, you know that self.__dict__
is a dictionary. And if you apply values to a dictionary, you will be returned a view object.
01:41 A view object is a list, and it’s a list that contains tuples of key-value pairs. And it will be the key-value pairs of the dictionary that values was applied to.
01:52
So in your case, it will be the key-value pairs of the __dict__
dictionary. So that’s a list. And therefore in line 13, you convert that to a tuple by applying tuple()
to that, and therefore you will have a tuple as the method suggests.
02:12
So that is the first example where you apply values to the whole dictionary. But if you return your attention to the __str__()
dunder method in line 7 to 10, you’ll work on an example that doesn’t use the dictionary as a whole, but that uses the individual entries within the dictionary.
02:33
So the __str__()
dunder method returns an f-string on lines 8 to 10, and this f-string uses the values of the instance attributes. For example, in line 8, there is the instance attribute of the name, first_name
, and the f-string uses the value of first_name
on line 9.
02:55
It uses the value of last_name
, and on line 10, it uses the value of age
.
03:03
So in your string, you have the names of the attributes, but you actually need the values of those attributes. And that, as you might remember, is of course exactly what the .__dict__
attribute does.
03:16
It stores as key-value pairs the names of attributes and the values of attributes. So for example, there will be a key in that dictionary by the name of first_name
, and behind that key will be stored the value of first_name
.
03:32
So what actually happens in the background is Python accesses the __dict__
dictionary
03:39
and it knows that there is an attribute by the name of first_name
, and that first name will therefore be a key of the dictionary. So if I know the name of the key, I can retrieve the value sitting behind the key using the method that we always do with dictionaries, which is the square brackets.
03:59
Of course, the same thing applies to last_name
on line 9. So in the background, Python will go to the dictionary.
04:06
It knows that there’s a key called last_name
, so it can then retrieve the value of that last name by just using the square brackets.
04:16
And of course, the same thing can be done for age
. Python in the background goes to .__dict__
04:22
and it then uses square brackets to retrieve the value of the age
attributes.
04:28
So this is your second example of how you can use __dict__
in real life. And in the next lesson, it’s time to test out the code.
Become a Member to join the conversation.