WHT

Python Property Example | Python Oop Tutorial 6: Property Decorators – Getters, Setters, And Deleters

Corey Schafer

Subscribe Here

Likes

19,868

Views

589,737

Python Oop Tutorial 6: Property Decorators - Getters, Setters, And Deleters

Transcript:

Hey, there, how’s it going? Everybody in this video we’re going to be learning how to use the property decorator. Now this allows us to give our class attributes getter setter and a leader functionality like may have seen in some other languages. So if you’ve been following along with my object-oriented video so far, you may have noticed that. I stripped down the employee class here a bit, and that’s just so we can focus on these attributes without the other code. Getting in the way. Okay, So so far with our employee class. Some of you may have noticed that our email attribute depends on our first name and our last name. So when we create our employee object here, it comes into our and NIP method and it sets the first name. The last name. Then it sets the email to our first name Dot last name @ email com, and also we have this full name method down here, which prints out the current first-name and Lastname together. So if I go down here and create this simple employee object and then print out the first name email and full name, you can see that all of these are what we would expect, so the first name is. John, the email is John Smith at email comm and the full name is both of those together. So let’s go here and now let’s set our employee dot first-name. I’m just gonna set that first name equal to Jim and rerun this. So you can see down here that the first name was changed to Jim, but the email still has our old first-name. Now the full name method here doesn’t have this problem because every time we run the full name method, it comes in here and grabs the current first-name and Lastname. So what if the people who are using our class said that we need to fix this, so they don’t want to change the email every time they change the first name or the last name, so they want us to make it to where it updates the email automatically when either the first name or the last name has changed. Now your first thought there might be to just create an email method, Just like we did here with our full name. But the problem with that is that it will break the code for everyone currently using the class so they would have to go through and change every instance of the email attribute with an email method. Now this is usually where people from other languages like Java will bring up the benefits of getter and setter methods and it’s a good point because this is where getter and setter really come in handy, but we have the ability to do this within Python using the property decorator. Now the property decorator allows us to define a method, but we can access it like an attribute, so for example, let’s go ahead and pull this email attribute out into a method similar to our full-name method. So I’m actually just going to copy the full name method here and paste this in, and I’m going to call this email, but instead of printing this out like the first name, I’m going to print this out like our email would show up so the first name Dot last name @ email, comm. And so now I’m just going to come up here and remove our email attribute and save that so right now our email method is similar to our full-name method. So each time we ran it. It would get the current first name and last name, but we’d also have to go through and change our code to where every email attribute is a method call. So for example, when I’m printing this out down here out instead have to go down here and add parentheses to this dot email. And so now if I run this here, you can see that this solved our problem where it set our email address to the new first name, but this also means that anyone using our class would have to change their code also. So that’s not what we want, so let’s go ahead and take these parentheses back off so in order to continue accessing email like an attribute. I can just add a property decorator above this method, So I’m going to go ahead and add this on. So this is just at property and just by making that small change. Now if I run our code, then you can see that that works, so we’re defining our email in our class like it’s a method, but we’re able to access it like an attribute and we could do this just as easily with full name as well. So if I was to add this property decorator onto full name and then take off these parentheses there and run that then you can see that that works also now. I know that goes against what I said about changing the code, but this is just an example. And I also wanted to use the full name to show you. An example of you can use a setter, so let’s say, for example, that we wanted the ability to say employee one dot full name and set it equal to. Let’s just say quarry. Schaefer, and let’s say that by setting this full name that we also wanted it to change our first name, our last name and our email and right now we can’t do this, so if we only have the full name with the property decorator and I try to set this like this if I run this here, you can see that we get an error there, and if we look at the error, it says that we can’t set the attribute. Okay, so for now. I’m just going to comment this out and rerun that so in order to do what we were trying to do there. We’re going to have to use a setter, and that’s going to be another decorator now. This might look a little strange, but the name that we’re going to use for our setter is going to be the name of the property. So in this case, it’s going to be full name, so I’m going to say at full name Dot setter, and then underneath this decorator, we just need to create another method with the same name, so this is going to be a method with full name. This is going to take in self and I’m going to call this name now. This name value here is the value that we are trying to set. So in this case down here, it would be this full name here, so I’m going to come in here and we want to set the first name and the last name using this full name here, so what we can do is we can just split that name that we pass in on the space separator, so I’ll say first last is equal to name not split will split on that space, so that’s going to split that name into two parts and the first part before the space will be the first name and the second part after the space will be the last name. So now we want to set our employee’s first name and last name equal to those values, so I can just say self dot first is equal to first and self dot last is equal to last. So now that I have that setter if I come down here and uncomment out this line that gave us an error before and rerun that then you can see that that works. So what happened here? Is that whenever we set this full name equal to this name, it came into our setter here, and it parsed the names from that value that we set and then it set our first name and last name and since we set the first name and last name, even when we printed out our email, it came in here and grabbed those correct values. They can also make a deleter in the same way, So let’s say that if I was to delete the full name of our employee that I wanted to run some kind of cleanup code so to do this. I’m just going to copy our setter here and paste this in below. But instead of a setter, this is going to be a leader for full name, the leader, and this won’t be accepting any other values other than self and Ill. Just go ahead and remove these here and just so that we can see this doing something within here. I’m just going to say, delete name, and I’m also just going to do. A self dot. First is equal to none and a self dot last is equal to none. So the deleter code there is what gets run whenever we delete an attribute, so at the very bottom here if I do a delete of employee one dot full name and run that then you can see here that it came in and ran that print statement that was in our deleter and also set our first name and last name equal to none values. Okay, so. I think that’s going to do it for this video. I hope you got some ideas for how you can use the property decorator within your classes now. This is a nice feature because it allows us to access attributes without putting getters and setters everywhere, but if we need that functionality, then it’s easy to add in with the property decorator. And if you do this correctly, then people using our class won’t even need to change any of their code because they’ll still be able to access those attributes in the same way that they did before. Now that wasn’t exactly the case in our example here because I changed the full name method into an attribute, but only did that because I wanted to show how we could use the full name as a setter to change the first name and the last name. So you won’t want to be careful with changes like that If other people are already using your classes. So if anyone has any questions about what we cover in this video, then feel free to ask in the comment section below. And I’ll do my best to answer those. If you enjoy these tutorials on the bike to sport, then there are several ways you can do that now. The easiest ways to simply like the video and give it a thumbs up and also it’s a huge help to share these videos with anyone who you think would find them useful. And if you have the means, you can contribute through Patreon and there’s a link to that page in the description section below, be sure to subscribe for future videos. And thank you all for watching.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more