Python OOP Tutorial 6: Property Decorators – Getters, Setters, and Deleters
Video Title: Python OOP Tutorial 6: Property Decorators – Getters, Setters, and Deleters
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 employees 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 I’ll 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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Thank you for your efforts! I just wanted to know what is the code editor you used!
If we write: emp_1.fullname = 'Mo Lin' it gives no error. It just adds fullname as an instance argument to emp_1. And then when we call : emp_1.fullname() it gives an error. It seems the fulltime argument overwrites the fulltime() method.
Great Explanation. 🙌
Beautiful explanation!
It has been at least year since I have given up on OOP! This was exactly what I needed for such a long time.
Need more videos on other concepts like abstraction and encapsulation. Please add.
Hello, I did not quite understand why we can't just create an email method. Can someone re/further explain this ? thanks
Amazing video! Thank you.
coming to its end made me emotinal lol, Thanks Corey!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This was a great tutorial series. Thank you!!
I mean, you're still not explaining why email is not dynamically updated. The question you answer is: How to update that value without refactoring my whole code? However, you don't explain why it is a static value. That would have been helpful, because right now I still don't know why that email value remains static.
Even after 7 years, this series about OOP is amazing. Thank you Corey
in 2024, i have a issue, how i can know if @property what do
your videos are a godsend. thank you so much
Thank you soo much
Completed the entire series. It was really great. Thank you very much!
way clearer than what i got at college, thank you!
i watched the whole series and thank you so much. You recorded this 7 years ago and still soo good soo understandable, finally all questions and blurs in my mind are gone.
Thanks Corey for the valuable contents!
Thank you for this amazing tutorials🥺🙏💓
It helped alot thank you ❤❤❤❤❤
What a amazing video! Thank you very much!
Thank you
8:00
Watching in 2024 and still learnt alot.
Thanks Mr Corey!
Thank you very much Corey, I just discovered your channel and it's great.