Python OOP Tutorial 6: Property Decorators – Getters, Setters, and Deleters

///Python OOP Tutorial 6: Property Decorators – Getters, Setters, and Deleters

Python OOP Tutorial 6: Property Decorators – Getters, Setters, and Deleters

FavoriteLoadingAdd to favorites

In this Python Object-Oriented Tutorial, we will be learning about the property decorator. The property decorator allows us to define Class methods that we can access like attributes. This allows us to implement getters, setters, and deleters. Let’s get started.

Python OOP 1 – Classes and Instances – https://youtu.be/ZDa-Z5JzLYM
Python OOP 2 – Class Variables – https://youtu.be/BJ-VvGyQxho
Python OOP 3 – Classmethods and Staticmethods – https://youtu.be/rq8cL2XMM5M
Python OOP 4 – Inheritance – https://youtu.be/RSl87lqOXDE
Python OOP 5 – Special (Magic/Dunder) Methods – https://youtu.be/3ohzBxoFHAY
Python OOP 6 – Property Decorators – https://youtu.be/jCzT9XFZ5bw

The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Object-Oriented

If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account:
https://www.patreon.com/coreyms

Or a one-time contribution through PayPal:
https://goo.gl/649HFY

If you would like to see additional ways in which you can support the channel, you can check out my support page:

You can find me on:
My website – http://coreyms.com/
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Google Plus – https://plus.google.com/+CoreySchafer44/posts
Tumblr – https://www.tumblr.com/blog/mycms

By |2017-05-01T12:20:08+00:00April 27th, 2017|Programming Video Tutorials, Python Video Tutorials|50 Comments

50 Comments

  1. Hamza mungadi April 27, 2017 at 12:52 am - Reply

    Thank for your tutorial

  2. Lâm Văn Duy April 27, 2017 at 12:53 am - Reply

    I’m wating for your new video.Do you have any course online?Tks!

  3. Kaoutar ZAIA April 27, 2017 at 12:54 am - Reply

    Awesome, All make sense now … thanks a lot Corey!

  4. Opiyo Sebastian April 27, 2017 at 12:55 am - Reply

    great job!!! Got the understanding!

  5. Saswat Dash April 27, 2017 at 12:55 am - Reply

    How all decorators @property, @fullname.setter and @fullname.deleter contains the same method ‘def fullname()’. without errors.

  6. Jack Chan April 27, 2017 at 12:55 am - Reply

    what IDE is this?

  7. Maziar Ghorbani April 27, 2017 at 12:56 am - Reply

    absolutely fantastic.

  8. Rolando Villca April 27, 2017 at 12:56 am - Reply

    Amazing, finally I have understood. Thank you so much.!!!
    Do you have video about Threading? Also, could you make a video about dependency injection sample?

  9. Mohamed Ali AbdElslam April 27, 2017 at 12:59 am - Reply

    Great , corey I want ask can you intro flask tutorial , i wash some time on your video to used

  10. Freethinking Влади́мир April 27, 2017 at 1:02 am - Reply

    Why can’t or won’t you do this:

    @fullname.setter
    def fullname(self, name):
    self.first, self.last = name.split(‘ ‘)

    So the result would be that we don’t have to do this intermediary step:
    self.first = first
    self.last = last

  11. Janardhan N April 27, 2017 at 1:06 am - Reply

    Great Tutorial…

  12. Rajesh Goyal April 27, 2017 at 1:07 am - Reply

    Must watch python videos. Thanks Corey.

  13. Yuval Mor April 27, 2017 at 1:08 am - Reply

    Hey, i’ve done the same thing but got other output :

    @fullname.setter
    def fullname(self, name):
    name, last_name = name.split(‘ ‘)
    self.name = name
    self.last_name = last_name

    emp1 = employee(‘Chana’, ‘or’)

    emp1.fullname = ‘Jack Or’

    print emp1.fullname
    print emp1.name
    print emp1.email

    the output :
    Jack Or
    Chana
    Chana.or@email.com

    Why is that? thanks !

  14. Ronald Cheng April 27, 2017 at 1:10 am - Reply

    Nice video. Clarifies my confusing regarding Google Cloud Platform’s endpoints-proto-datastore @EndpointsAliasProperty usage.

  15. Tadae323 April 27, 2017 at 1:11 am - Reply

    Seriously, checked the whole Internet and your tutorials are the best.

  16. Aram Hamidi April 27, 2017 at 1:12 am - Reply

    Why my @property email getter doesn’t work? Is it python 2 vs 3 thing?

  17. Jeffrey Apiado April 27, 2017 at 1:12 am - Reply

    Nicely explain and clear. Good work!

  18. Khoa Tran April 27, 2017 at 1:15 am - Reply

    guys, python is alot messier than scala

  19. Saurabh Kansal April 27, 2017 at 1:15 am - Reply

    Before starting these videos, I don’t have any knowledge of classes. But now i got the concept and can understand the source codes in better way. Thanks Corey… Thanks a lot..

  20. Murmad Man April 27, 2017 at 1:15 am - Reply

    3:50 define function and as simple as adding that property decoratory before it. You do it before multiple functions if you have multiple functions
    5:35 create a method with setter

  21. Prantick Saha April 27, 2017 at 1:17 am - Reply

    helpful ..thanks 🙂

  22. Viraj Thakrar April 27, 2017 at 1:17 am - Reply

    Amazing series of tutorials. it helped a lot to clear most of the OOPs concepts. not only in python prespective. but also for the overall Object Oriented Paradigm and some of basic class design ideas as well.

    if you dont mind Can you share some of your web application development knowledge using python? Or can you share some resource which can explain concepts as well as you are explaining and covering?

  23. Pablo Napán M. April 27, 2017 at 1:18 am - Reply

    You create the getter and setter for the CLASS variable. Aren’t we supposed to create getters and setters for the instance variables?

  24. Gwith April 27, 2017 at 1:19 am - Reply

    This was good, thanks!

  25. Boris Đurkan April 27, 2017 at 1:20 am - Reply

    Great tutorial!

  26. Peter Nilson April 27, 2017 at 1:22 am - Reply

    One of the clearest explanations of this that I’ve seen before. Thanks very much!

  27. pramesh shakya April 27, 2017 at 1:23 am - Reply

    Could someone explain why the first name in the email didn’t change? but calling the fullname() method changed it?

  28. Dmitriy Devjatykh April 27, 2017 at 1:23 am - Reply

    Is there a way to make class attribute a proprty and give it setter and getter?
    Correct me if I am wrong, but you made property for instance attrirubutes only

  29. T Pham April 27, 2017 at 1:26 am - Reply

    Your " del emp_1.fullname " isn’t working. Where did you come up with del command? Must be calling to some libraries?

  30. Ravi Teja Kondisetty April 27, 2017 at 1:27 am - Reply

    Hey Corey,
    I absolutely loved the series of python videos. I appreciate it. One thing I’d like to ask is "Where can I implement or practice these concepts?" I am a beginner but I can apply the logics well. Please suggest few resources.

  31. Eric P April 27, 2017 at 1:28 am - Reply

    Very nice tutorial!
    I have a question.
    So is it usually a good practice to use properties instead of just class variables if those properties depend on other variables?

  32. Yehoshua Kahan April 27, 2017 at 1:28 am - Reply

    Hi, Corey. I’ve been going through your videos these past couple of days to help me keep up with my programming class. Thank you.

    I do have a couple of questions. First off, why did you change the self.email and self.fullname attributes via @property tag instead of adding lines to the __init__ function to the following effect?
    self.email = self.first + "." + self.last + "@email.com"
    self.fullname = self.first + " " + self.last

    Second, in your setter, why did you use three lines of code to set the fullname attribute instead of one, like so?
    self.first, self.last = name.split(" ")

  33. Michael D.Weller April 27, 2017 at 1:28 am - Reply

    Thanks for the videos on OOP using Python. It’s really helped me understand how classes work in Python.

  34. Charles Carriere April 27, 2017 at 1:32 am - Reply

    Love your videos – so clear and well-paced! Quick question – you don’t explicitly define ‘del’ in the property decorator @fullname.deleter – so I am a bit confused. does ‘del’ always call the ‘deleter’ decorator? if the answer is YES, is there also shorthand for ‘set’ and ‘get’?

  35. Brandon Doyle April 27, 2017 at 1:32 am - Reply

    Very straightforward and simple.

  36. Murmad Man April 27, 2017 at 1:34 am - Reply

    Something I don’t get. You start the video by saying ‘hey pulling the email address results in using the non-updated name’ …so how does the setter method that splits names resolve that dilemma?

    Also why not just use a sub function in full name function, to delete like you did in the regular decorator video?

  37. Younes PhD Henni April 27, 2017 at 1:36 am - Reply

    Hi
    You did not talk about getters !

  38. Arvind Ks April 27, 2017 at 1:37 am - Reply

    Hi Corey,
    I am using python2.7 . The setter example is not working as expected.
    Could you please advise what should be done. It should ideally print "Jack Daniel’
    in all 3 lines of the output. Thx in advance

    Script
    Wed Feb 15 17:20:14 root@ansible:/var/tmp/python380 cat class14.py
    #!/usr/bin/python
    import string,datetime

    class employee:
    def __init__(self,first,last):
    self.first = string.lower(first)
    self.last = string.lower(last)

    @property
    def email(self):
    return ‘{}.{}@gmail.com’.format(self.first,self.last)

    @property
    def fullname(self):
    return ‘{} {}’.format(self.first, self.last)

    @fullname.setter
    def fullname(self, name):
    first, last = name.split(‘ ‘)
    self.first = first
    self.last = last

    emp1 = employee(‘John’, ‘Doe’)

    emp1.fullname = ‘Jack Daniel’

    print emp1.first
    print emp1.email
    print emp1.fullname
    Wed Feb 15 17:20:17 root@ansible:/var/tmp/python381

    Output
    Wed Feb 15 17:20:17 root@ansible:/var/tmp/python381 python class14.py
    john
    john.doe@gmail.com
    Jack Daniel
    Wed Feb 15 17:20:20 root@ansible:/var/tmp/python382

  39. Danny Chung April 27, 2017 at 1:38 am - Reply

    very great!

  40. Khalid Mohammad April 27, 2017 at 1:38 am - Reply

    Hi , after i finished learning from this Great video, what should i practice for better OOP python? any recommended books?

    It would really be useful. Thanks!

  41. aa musiclover April 27, 2017 at 1:38 am - Reply

    I understood decorators in one go !!!!!!!! Great job 🙂 Thanks a lot !

  42. ABC April 27, 2017 at 1:38 am - Reply

    Great tutorial Corey. I learnt a lot of new things that I did not know existed in Python.

  43. MrWhaleFarmer April 27, 2017 at 1:38 am - Reply

    Hi, Corey, great tutorial. However I’m still not fully understanding the @property, if u have a few minutes can u reply with a quick definition. Appreciated.

    class Background:
    def __init__(self, x, y, file):
    self.x = x
    self.y = y
    self.Img = pygame.image.load(file)
    self.w = self.Img.get_rect().width
    self.h = self.Img.get_rect().height
    self.Rect = pygame.Rect(self.x, self.y, self.w, self.h)

    def render(self):
    DISPLAYSURF.blit(self.Img, (self.x, self.y))

    @property
    def sprite(self):
    return pygame.Rect(self.x, self.y, self.w, self.h,)

    So thats my code, i just need to fully explain everything, thanks again!

  44. Alexandre Damião April 27, 2017 at 1:41 am - Reply

    great videos man! thanks a lot!

  45. Mujahid Rasheed April 27, 2017 at 1:42 am - Reply

    Great post..
    For PYthon 2: the Class must inherit from the ‘object’ class to able able to use decorators.
    example: class Employee(object)
    learnt it the hard way..

  46. Ivandro Ismael April 27, 2017 at 1:42 am - Reply

    Very well explained, thank you 🙂

  47. Sayyar Khan April 27, 2017 at 1:43 am - Reply

    Sir, Corey!
    Do you had any tutorial series on cloud computing?

  48. sabiq al hasby April 27, 2017 at 1:44 am - Reply

    Thanks sir, now i know what is Setters n Deleters. can u explain the "getters" ? cause in the video was not explained.

  49. Swadhikar C April 27, 2017 at 1:46 am - Reply

    Hi Corey,

    Your videos are none other than best! Simple and easy to understand. I am a beginner to Python and have a question in deleter. The example you have shown is used to delete a property. Can you explain how to delete an instance itself? example: del emp_1?

    Thanks a lot for your videos!

  50. Wissam Ali April 27, 2017 at 1:50 am - Reply

    Thank you so much for these wonderful python oop tutorials. All makes sense now. Could you please make a video describing the difference between oop inheritance and composition.

Leave A Comment

*