Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Decorators are special functions that return other functions and they are used to modify the way that a function works. There's a special syntax for using decorators in Python. Let's take a look at how that works. Let's make a working copy of decorators.py and we will call it decorators-working.py and we will open that up, and we will see we have our Duck example here. This one has a properties variable that's getting set by the keyword arguments, and it has a way of setting properties and getting properties.
Down here we have set a property in our constructor and we were getting that property in this print function. So if we run this, we see that we are printing out that color. Now, one of the most common uses for decorators in Python is to create accessor methods for variables. For examples, if I wanted special accessors for color, I can define a function called color and configure it like a getter. So I'll return self.properties.get ('color') and with a default value of None.
What turns this into an accessor is this decorator function that looks like that. With an @ sign and the word property, and that turns this into an accessor for the variable called color. Then I can create a setter by saying @color.setter and def and I name it exactly the same thing, color. This is all special syntax that's designed just for this purpose. I set an argument.
We will call it c and we will set self.properties sub 'color' = c, and now we have our setter. Finally, color.deLeter, sub 'color' again, and delete self.properties sub 'color' and what this allows us to do is this.
I can see donald.clolor = 'blue'. I don't need to initialize it here any more. So if I save that and run it, we will see that it still works the way that we want it to. Here I can just say donald.color. Now, this might look like I went to a lot of trouble to just be able to use a property that I could have just used without all of this trouble and it would have set the property in the object, and read the property from the object.
But the beauty here is that now it's under the control of the object. And so if I wanted to do some thing else in here when I set this. If I wanted to save it to a database or if I wanted to create a database based on a filename, which is an example of how I have used this recently. Then in the deLeter, if I wanted to close the database based on that filename. So now I can do all of those things, but I still have this convenient syntax.
So this is the most common use of decorators and this is a little bit of the power of decorators as well, is it they can fundamentally change the behavior of a function, because here I have got function methods which are operating a setters and getters, but I am not calling them as functions. I am calling them in this simple normal properties syntax. So that's the power of decorators and that's a simple example of how you can use decorators in your objects.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99426 Viewers
56 Video lessons · 112660 Viewers
71 Video lessons · 81488 Viewers
131 Video lessons · 39119 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.