1
00:00:00,000 --> 00:00:03,990
Decorators are special functions that return other functions and they are used

2
00:00:04,000 --> 00:00:06,990
to modify the way that a function works.

3
00:00:07,000 --> 00:00:09,990
There's a special syntax for using decorators in Python.

4
00:00:10,000 --> 00:00:12,990
Let's take a look at how that works.

5
00:00:13,000 --> 00:00:17,990
Let's make a working copy of decorators.py and we will call it

6
00:00:18,000 --> 00:00:22,990
decorators-working.py and we will open that up, and we will see we have

7
00:00:23,000 --> 00:00:25,990
our Duck example here.

8
00:00:26,000 --> 00:00:29,990
This one has a properties variable that's getting set by the keyword arguments,

9
00:00:30,000 --> 00:00:33,990
and it has a way of setting properties and getting properties.

10
00:00:34,000 --> 00:00:37,990
Down here we have set a property in our constructor and we were getting that

11
00:00:38,000 --> 00:00:40,990
property in this print function.

12
00:00:41,000 --> 00:00:43,990
So if we run this, we see that we are printing out that color.

13
00:00:44,000 --> 00:00:48,990
Now, one of the most common uses for decorators in Python is to create accessor

14
00:00:49,000 --> 00:00:50,990
methods for variables.

15
00:00:51,000 --> 00:00:56,990
For examples, if I wanted special accessors for color, I can define a function

16
00:00:57,000 --> 00:01:01,990
called color and configure it like a getter.

17
00:01:02,000 --> 00:01:10,990
So I'll return self.properties.get ('color') and with a default value of None.

18
00:01:11,000 --> 00:01:17,990
What turns this into an accessor is this decorator function that looks like that.

19
00:01:18,000 --> 00:01:23,990
With an @ sign and the word property, and that turns this into an accessor for

20
00:01:24,000 --> 00:01:25,990
the variable called color.

21
00:01:26,000 --> 00:01:36,990
Then I can create a setter by saying @color.setter and def and I name it exactly

22
00:01:37,000 --> 00:01:38,990
the same thing, color.

23
00:01:39,000 --> 00:01:44,990
This is all special syntax that's designed just for this purpose. I set an argument.

24
00:01:45,000 --> 00:01:54,990
We will call it c and we will set self.properties sub 'color' = c, and now we have our setter.

25
00:01:55,000 --> 00:02:11,990
Finally, color.deLeter, sub 'color' again, and delete self.properties sub 'color' and what

26
00:02:12,000 --> 00:02:15,990
this allows us to do is this.

27
00:02:16,000 --> 00:02:20,990
I can see donald.clolor = 'blue'.

28
00:02:21,000 --> 00:02:25,990
I don't need to initialize it here any more.

29
00:02:26,000 --> 00:02:30,990
So if I save that and run it, we will see that it still works the way that we want it to.

30
00:02:31,000 --> 00:02:33,990
Here I can just say donald.color.

31
00:02:34,000 --> 00:02:38,990
Now, this might look like I went to a lot of trouble to just be able to use a

32
00:02:39,000 --> 00:02:42,990
property that I could have just used without all of this trouble and it would

33
00:02:43,000 --> 00:02:47,990
have set the property in the object, and read the property from the object.

34
00:02:48,000 --> 00:02:54,990
But the beauty here is that now it's under the control of the object. And so if

35
00:02:55,000 --> 00:02:57,990
I wanted to do some thing else in here when I set this.

36
00:02:58,000 --> 00:03:03,990
If I wanted to save it to a database or if I wanted to create a database based

37
00:03:04,000 --> 00:03:08,990
on a filename, which is an example of how I have used this recently.

38
00:03:09,000 --> 00:03:12,990
Then in the deLeter, if I wanted to close the database based on that filename.

39
00:03:13,000 --> 00:03:17,990
So now I can do all of those things, but I still have this convenient syntax.

40
00:03:18,000 --> 00:03:21,990
So this is the most common use of decorators and this is a little bit of the

41
00:03:22,000 --> 00:03:27,990
power of decorators as well, is it they can fundamentally change the behavior of

42
00:03:28,000 --> 00:03:32,990
a function, because here I have got function methods which are operating a

43
00:03:33,000 --> 00:03:37,990
setters and getters, but I am not calling them as functions. I am calling them

44
00:03:38,000 --> 00:03:40,990
in this simple normal properties syntax.

45
00:03:41,000 --> 00:03:45,990
So that's the power of decorators and that's a simple example of how you can use

46
00:03:46,000 --> 00:03:56,000
decorators in your objects.

