1
00:00:00,450 --> 00:00:04,500
Previously, we looked at simple Python decorator functions.

2
00:00:05,070 --> 00:00:08,970
And the reason why it's simple is because all that we're doing when we're

3
00:00:08,970 --> 00:00:13,350
calling the function in the decorator is we're just calling it straight up.

4
00:00:14,040 --> 00:00:18,480
But this doesn't take into account in the cases where, for example,

5
00:00:18,480 --> 00:00:23,480
we want to add a condition before we called the function or even more advanced.

6
00:00:23,910 --> 00:00:27,510
What if we needed to call the function with some sort of inputs?

7
00:00:27,690 --> 00:00:31,860
How would we get hold of that? If it was inside a decorator?

8
00:00:32,369 --> 00:00:36,510
So let's say that we had a class called user.

9
00:00:37,140 --> 00:00:39,090
Now, if you are not familiar with classes,

10
00:00:39,150 --> 00:00:43,260
I strongly recommend going back to the lessons where we covered classes and

11
00:00:43,260 --> 00:00:45,180
Object in great detail.

12
00:00:45,220 --> 00:00:48,960
We did over several days just to make sure that you're up to date with that

13
00:00:49,020 --> 00:00:52,830
before you continue. But this is a pretty simple class.

14
00:00:53,130 --> 00:00:56,910
All it does is it has two properties, a name, property,

15
00:00:57,120 --> 00:00:58,890
and it is logged in property.

16
00:00:59,430 --> 00:01:03,240
So basically what we're trying to simulate is a condition where let's say we had

17
00:01:03,240 --> 00:01:08,240
a blog website and we had these user objects and the user has a username and

18
00:01:09,540 --> 00:01:12,660
also some sort of property that keeps track of whether,

19
00:01:12,660 --> 00:01:16,950
or if the user is going onto the website and not logged in,

20
00:01:17,070 --> 00:01:19,110
or whether if they all have already logged in.

21
00:01:20,520 --> 00:01:23,280
So certain methods on the website,

22
00:01:23,370 --> 00:01:27,840
you can only be achieved by users who are logged in it's. For example,

23
00:01:27,840 --> 00:01:30,540
if they wanted you to create a new blog post we'll,

24
00:01:30,540 --> 00:01:33,270
then that user has to be checked.

25
00:01:33,660 --> 00:01:38,660
And we have to make sure that they are in fact logged in and authenticated to

26
00:01:39,180 --> 00:01:41,310
create a new blog post.

27
00:01:41,790 --> 00:01:46,790
If I go ahead and create a new user from the user class,

28
00:01:47,580 --> 00:01:50,940
then I can give it a name, which I'll put as Angela.

29
00:01:51,540 --> 00:01:56,540
And then I could create the blog post and pass in my user.

30
00:01:58,080 --> 00:02:01,710
Now, when I hit run, you'll see, this is Angela's new blog,

31
00:02:01,740 --> 00:02:04,230
post being printed right here.

32
00:02:05,460 --> 00:02:10,050
Now what if I wanted to create a decorator function that could be used to

33
00:02:10,050 --> 00:02:14,970
decorate any function on our website that requires authentication.

34
00:02:15,150 --> 00:02:20,150
So it requires that this users is logged in property is set to true.

35
00:02:22,260 --> 00:02:26,400
So let's say that I decided to create my decorator function right here,

36
00:02:26,430 --> 00:02:30,600
which I'll call the is authenticated decorator.

37
00:02:32,220 --> 00:02:36,690
And this is of course going to need to take a function as an input.

38
00:02:37,020 --> 00:02:39,960
And then inside, we've got our wrapper function.

39
00:02:41,190 --> 00:02:46,110
Now inside our wrapper function is where we call this function.

40
00:02:46,110 --> 00:02:48,090
That's the past and right here.

41
00:02:48,810 --> 00:02:51,660
And finally to complete our decorator.

42
00:02:51,690 --> 00:02:56,690
We have to return the wrapper as a function without the parentheses.

43
00:02:57,990 --> 00:03:01,540
Now, in order to check for the is authenticated,

44
00:03:01,600 --> 00:03:04,450
what we actually want to do is we want to say, well,

45
00:03:04,480 --> 00:03:09,480
if the user.is logged in is equal to true will in that case,

46
00:03:12,040 --> 00:03:16,570
we're going to trigger the function. But if it's actually not true, well,

47
00:03:16,570 --> 00:03:19,390
then we're not going to trigger the function. Now,

48
00:03:19,420 --> 00:03:23,080
the only problem here is that this user is undefined.

49
00:03:23,800 --> 00:03:26,650
We were only able to pass in the function,

50
00:03:27,040 --> 00:03:32,040
but what if we needed to pass in some arguments associated with that function?

51
00:03:33,730 --> 00:03:38,170
Notice here that all function create blog posts, which is what we want to add.

52
00:03:38,170 --> 00:03:42,910
The decorator to accepts an input in the form of the user. Object.

53
00:03:43,480 --> 00:03:48,280
If I decorate this function with the is authenticated decorator,

54
00:03:48,850 --> 00:03:50,830
and I tried to run this,

55
00:03:51,190 --> 00:03:55,840
then it's not really gonna work because that user is not defined.

56
00:03:56,440 --> 00:04:01,440
And it tells us that the wrapper function takes zero positional arguments,

57
00:04:03,460 --> 00:04:05,590
but one was given.

58
00:04:06,220 --> 00:04:09,580
And the one that's given is this user right here,

59
00:04:10,990 --> 00:04:15,990
this era might already be a good hint for us to figure out how to fix this.

60
00:04:17,709 --> 00:04:22,710
One of the things that we talked about in previous lessons is the concept of OGs

61
00:04:22,990 --> 00:04:26,470
and clogs, unlimited positional arguments.

62
00:04:26,890 --> 00:04:29,830
Normally all add it in as asterix,

63
00:04:29,890 --> 00:04:34,890
OGs and unlimited keyword arguments are usually added in as two Astrix and then

64
00:04:37,360 --> 00:04:41,380
Crocs. So now we can have all function,

65
00:04:41,740 --> 00:04:45,130
which is decorated with this decorate to function.

66
00:04:45,790 --> 00:04:47,590
And when we call the function,

67
00:04:47,740 --> 00:04:52,740
we could provide some inputs and those inputs can be tapped into via these OGs

68
00:04:55,390 --> 00:04:57,820
and Crocs. Now, in this case,

69
00:04:57,850 --> 00:05:02,170
this is a positional argument and it's the argument App positions zero.

70
00:05:02,800 --> 00:05:06,790
So instead of using this user, which is what we would do,

71
00:05:06,790 --> 00:05:09,910
if we were inside this function, instead,

72
00:05:09,940 --> 00:05:12,400
we would use the wrappers OGs,

73
00:05:13,330 --> 00:05:17,260
and then we would use the position set to zero.

74
00:05:18,040 --> 00:05:23,040
So now it's going to look at the function that is going to be passed in which is

75
00:05:24,430 --> 00:05:26,170
going to have some inputs.

76
00:05:26,740 --> 00:05:31,740
And it's going to take that first position input and then see if it is logged in

77
00:05:32,380 --> 00:05:33,970
property is equal to true.

78
00:05:35,320 --> 00:05:39,760
Now that my wrapper is taking positional arguments and keyword arguments,

79
00:05:40,120 --> 00:05:45,120
and I can tap into the first positional argument by using Oxy zero.

80
00:05:45,700 --> 00:05:48,490
Well inside the rapt function,

81
00:05:48,820 --> 00:05:53,820
I can now also use this first argument in order to give it as an input to that

82
00:05:54,610 --> 00:05:55,443
function.

83
00:05:55,630 --> 00:06:00,440
Remember that this function is simply this create blog post function without the

84
00:06:00,440 --> 00:06:04,370
decorator. Now, if I go ahead and hit run,

85
00:06:05,240 --> 00:06:10,240
you will see that nothing happens unless I change this users is logged in

86
00:06:11,420 --> 00:06:15,740
property to equal. True. And now that that's true,

87
00:06:15,890 --> 00:06:18,080
then it's going to be checked right here.

88
00:06:18,410 --> 00:06:23,410
And then it's going to call the function because that user is now logged in this

89
00:06:24,740 --> 00:06:28,280
idea of being able to pass in positional arguments,

90
00:06:28,340 --> 00:06:30,170
as well as keyword arguments,

91
00:06:30,500 --> 00:06:34,310
by simply adding the odds and quarks to the wrapper function,

92
00:06:34,640 --> 00:06:39,640
and then using it inside the wrapper or using it to call the function itself is

93
00:06:40,670 --> 00:06:45,110
one step higher in terms of understanding Python,

94
00:06:45,110 --> 00:06:46,340
decorator functions.

95
00:06:47,120 --> 00:06:50,420
I recommend spending a few minutes looking at this code,

96
00:06:50,690 --> 00:06:54,350
which you can access at this URL, which is in the course resources,

97
00:06:54,830 --> 00:06:59,510
and then trying to recreate this functionality from scratch based on your

98
00:06:59,510 --> 00:07:03,680
understanding of what we just talked about in the next lesson,

99
00:07:03,740 --> 00:07:08,360
I've got a coding exercise for you so that you can try out this new advanced

100
00:07:08,360 --> 00:07:12,200
form of Python decorators and try creating one yourself.

