1
00:00:01,280 --> 00:00:02,840
Hi guys and welcome back.

2
00:00:02,840 --> 00:00:05,823
Let's look at the at syntax for decorators.

3
00:00:06,680 --> 00:00:09,020
Here we've got our code from the last video.

4
00:00:09,020 --> 00:00:11,280
We have our make secure function,

5
00:00:11,280 --> 00:00:13,060
that defines an inner function,

6
00:00:13,060 --> 00:00:15,220
that will be used to replace,

7
00:00:15,220 --> 00:00:17,790
the function that we want to make secure.

8
00:00:17,790 --> 00:00:19,660
This function checks the access level,

9
00:00:19,660 --> 00:00:22,250
and then returns a call to the original one,

10
00:00:22,250 --> 00:00:24,453
such as the get admin password function.

11
00:00:25,320 --> 00:00:28,540
Now I'll have you know that instead of doing this

12
00:00:28,540 --> 00:00:31,510
get admin password equal make secure of get admin password,

13
00:00:31,510 --> 00:00:32,680
which can have some problems,

14
00:00:32,680 --> 00:00:35,300
for example if you decide to call get admin password

15
00:00:35,300 --> 00:00:39,300
before that line runs you can use the at syntax,

16
00:00:39,300 --> 00:00:42,060
which does exactly the same thing.

17
00:00:42,060 --> 00:00:45,100
That is at make secure,

18
00:00:45,100 --> 00:00:48,410
if you put at make secure in front of or on top of

19
00:00:48,410 --> 00:00:52,070
a function definition that will prevent the function

20
00:00:52,070 --> 00:00:55,150
from being created as is and instead it will create it

21
00:00:55,150 --> 00:00:57,910
and pass it through the decorator, in one go.

22
00:00:57,910 --> 00:00:59,990
Though you no longer need that here

23
00:00:59,990 --> 00:01:02,260
and now you'll see that if we run this code

24
00:01:02,260 --> 00:01:05,320
you will get no admin permissions for Jose.

25
00:01:05,320 --> 00:01:07,190
Because at the moment I am a guest,

26
00:01:07,190 --> 00:01:09,380
but if I change this to an admin,

27
00:01:09,380 --> 00:01:11,103
then it will all work out.

28
00:01:12,600 --> 00:01:15,520
However there is a small problem with decorators as a whole.

29
00:01:15,520 --> 00:01:16,910
Both when you assign them

30
00:01:16,910 --> 00:01:19,030
and also when you use the at syntax.

31
00:01:19,030 --> 00:01:21,530
Which is that when you replace this function,

32
00:01:21,530 --> 00:01:24,022
by this one here,

33
00:01:24,022 --> 00:01:27,670
the name of the function changes.

34
00:01:27,670 --> 00:01:30,210
The get admin password it still exists

35
00:01:30,210 --> 00:01:32,570
as a name inside Python

36
00:01:32,570 --> 00:01:35,740
but it is no longer registered as a function internally.

37
00:01:35,740 --> 00:01:37,670
Now the secure function is registered

38
00:01:37,670 --> 00:01:39,400
as a function internally,

39
00:01:39,400 --> 00:01:41,220
and there are some libraries that will

40
00:01:41,220 --> 00:01:43,440
actually use that internal name.

41
00:01:43,440 --> 00:01:47,750
So, for example if you print get admin password dot name

42
00:01:47,750 --> 00:01:48,900
you'll see what I mean.

43
00:01:49,750 --> 00:01:53,870
The name of get admin password, is actually secure function.

44
00:01:53,870 --> 00:01:56,020
So, that is a bit of a problem,

45
00:01:56,020 --> 00:01:57,960
a problem that we have to solve.

46
00:01:57,960 --> 00:02:01,240
In addition, if you have any documentation attached

47
00:02:01,240 --> 00:02:04,100
to get admin password it will also be lost

48
00:02:04,100 --> 00:02:06,060
when you replace it with secure function,

49
00:02:06,060 --> 00:02:08,550
because this one doesn't have any documentation.

50
00:02:08,550 --> 00:02:10,500
To fix that problem all you have to do

51
00:02:10,500 --> 00:02:12,780
is actually use another decorator.

52
00:02:12,780 --> 00:02:15,493
So, will go into import functools,

53
00:02:16,720 --> 00:02:18,580
which is short for function tools,

54
00:02:18,580 --> 00:02:21,240
this is a built in module in Python,

55
00:02:21,240 --> 00:02:23,990
and then we're going to decorate our secure function.

56
00:02:23,990 --> 00:02:27,510
We're gonna say at functools dot wraps

57
00:02:27,510 --> 00:02:29,480
and then we're going to tell secure function

58
00:02:29,480 --> 00:02:32,860
that it is a wrapper for func.

59
00:02:32,860 --> 00:02:35,160
And therefore what this does is essentially

60
00:02:35,160 --> 00:02:37,730
it keeps the name and the documentation

61
00:02:37,730 --> 00:02:40,053
of this function if there are any.

62
00:02:40,930 --> 00:02:42,270
So the name is always gonna be there

63
00:02:42,270 --> 00:02:44,700
if we now run this code you can see

64
00:02:44,700 --> 00:02:46,810
that get admin password is what comes out.

65
00:02:46,810 --> 00:02:49,600
So that's what the functools wraps is for

66
00:02:49,600 --> 00:02:53,060
you need that on pretty much every decorator you write,

67
00:02:53,060 --> 00:02:54,910
so do get accustomed to using that.

68
00:02:54,910 --> 00:02:56,710
Remember, you wanna put it on top of

69
00:02:56,710 --> 00:02:58,700
the inner secure function

70
00:02:58,700 --> 00:03:01,643
not on top of this make secure decorator.

71
00:03:02,870 --> 00:03:04,960
Some terminology at this point by the way

72
00:03:04,960 --> 00:03:08,230
the make secure function is called a decorator

73
00:03:08,230 --> 00:03:10,930
because you can use it decorate a function.

74
00:03:10,930 --> 00:03:12,770
This here is not a decorator,

75
00:03:12,770 --> 00:03:15,570
that's just a function that will replace your other one.

76
00:03:17,630 --> 00:03:19,520
All right, that's everything for this video.

77
00:03:19,520 --> 00:03:22,160
I wanted to show you the at syntax for decorators

78
00:03:22,160 --> 00:03:25,840
and also functools wraps those two are essential

79
00:03:25,840 --> 00:03:28,300
when you're working with decorators in Python.

80
00:03:28,300 --> 00:03:29,610
Thanks for joining me in this one,

81
00:03:29,610 --> 00:03:30,660
hope you've learned something,

82
00:03:30,660 --> 00:03:32,410
and I'll see you in the next video.

