Modifying the .__dict__ Attribute
00:00
Previously you’ve learned how to inspect and use the .__dict__
attribute. In this lesson, you’ll see how to modify it. For that, please move back to the Person
class and you’ll be adding two methods to this class.
00:16
The first method is a method to update the .__dict__
dictionary and the second method will be to delete entries from it. But first, please start by typing def update_person
.
00:31
That is an instance method, so it takes self
as a first parameter and then it will need keyword arguments. So double star kwargs
. What will happen?
00:44
Well, you are dealing with a dictionary, so all you need to do really is to update the dictionary and you can do that by using the update()
functionality of a dictionary.
01:06
So what is going on here? So on line 15 you need to unpack kwargs
. So what is that? kwargs
is actually a dictionary that is created by Python in the background when you pass in keyword arguments.
01:19 So it’s a dictionary that holds as keys the keywords and as values the values of those keywords. Now the signature is such that these need to be individual values.
01:31
So you need to unpack the dictionary. You can’t just pass in a dictionary. To update a dictionary in line 16, you just use update()
applied to the dictionary and update()
actually expects a dictionary.
01:47
And kwargs
already is a dictionary, so you don’t need to do anything different. The second method to add is to remove entries from the dictionary.
02:03
again self
, and then you’re going to have to pass in a key
because we’ll need to know which attribute to remove.
02:11 Let’s do a check to see if the key’s actually in our dictionary
02:19
and only then remove it. So use del
, DEL and then self.__dict__
. And this is where we need the key
. So just as you did for update, where you just used the normal update()
functionality for a dictionary, to remove something, you use the normal del
functionality to remove entries from a dictionary.
02:43 And all that’s left to do now is to save the code and test it in your REPL. So please save and then restart your REPL.
02:53
As before, from person
in lowercase import Person
with a capital P, which is the class, create an instance. So steven
is a Person
.
03:06 First name “Steven”, last name “Loyens” and oh, can’t spell my own last name, and age, I’m still 21. Okay, that is as before. So the first thing to test is to update a certain attribute.
03:25
Let’s say it’s one year later and I have aged one year. So on steven
, I’m going to call the update_person()
method and I need keyword arguments.
03:38
So age
, I want to change that to 22, hit Enter. And now to see if this works. Let’s use the as_tuple()
method on the instance to see what is going on.
03:52
So as_tuple()
, don’t forget the parentheses. And now indeed Steven is 22 years old. Now interestingly, this update functionality that you have implemented using the update_person()
method actually also allows you to add attributes.
04:13
So steven.update_person()
.
04:18
So let’s assume that you would like to add gender as an attribute. This update_person()
method expects keyword arguments. So gender="male"
, and that is a string.
04:32
Okay? And now again call as_tuple()
to see what has happened in the background. So .as_tuple()
parentheses. And now you can see that indeed there is a fourth attribute here, which is gender
.
04:47
Well, you see male, you see the value of it. So you have learned that with the update_person()
method, which uses the update()
functionality on a dictionary, you can change existing attributes, but you can also add new ones.
05:03
And the final thing to check is that I can actually remove attributes as well. And for that on the instance call the remove_
attribute()
method.
05:16
And remember that that method, it doesn’t require keyword arguments. It requires a key. So the key of the attribute I’d like to remove is gender. So that is "gender"
in double quotation marks. Hit Enter.
05:30
And let’s try the as_tuple()
method again. So as_tuple()
on the instance, don’t forget parentheses. And again, you can see that now male
is no longer there.
05:41 So gender has been removed.
05:45
So you have learned that using standard dictionary functionality, you can modify the .__dict__
attribute. In the next lesson, we will summarize the findings of this course.
Become a Member to join the conversation.