Previously, we looked at simple Python decorator functions.
And the reason why it's simple is because all that we're doing when we're
calling the function in the decorator is we're just calling it straight up.
But this doesn't take into account in the cases where, for example,
we want to add a condition before we called the function or even more advanced.
What if we needed to call the function with some sort of inputs?
How would we get hold of that? If it was inside a decorator?
So let's say that we had a class called user.
Now, if you are not familiar with classes,
I strongly recommend going back to the lessons where we covered classes and
Object in great detail.
We did over several days just to make sure that you're up to date with that
before you continue. But this is a pretty simple class.
All it does is it has two properties, a name, property,
and it is logged in property.
So basically what we're trying to simulate is a condition where let's say we had
a blog website and we had these user objects and the user has a username and
also some sort of property that keeps track of whether,
or if the user is going onto the website and not logged in,
or whether if they all have already logged in.
So certain methods on the website,
you can only be achieved by users who are logged in it's. For example,
if they wanted you to create a new blog post we'll,
then that user has to be checked.
And we have to make sure that they are in fact logged in and authenticated to
create a new blog post.
If I go ahead and create a new user from the user class,
then I can give it a name, which I'll put as Angela.
And then I could create the blog post and pass in my user.
Now, when I hit run, you'll see, this is Angela's new blog,
post being printed right here.
Now what if I wanted to create a decorator function that could be used to
decorate any function on our website that requires authentication.
So it requires that this users is logged in property is set to true.
So let's say that I decided to create my decorator function right here,
which I'll call the is authenticated decorator.
And this is of course going to need to take a function as an input.
And then inside, we've got our wrapper function.
Now inside our wrapper function is where we call this function.
That's the past and right here.
And finally to complete our decorator.
We have to return the wrapper as a function without the parentheses.
Now, in order to check for the is authenticated,
what we actually want to do is we want to say, well,
if the user.is logged in is equal to true will in that case,
we're going to trigger the function. But if it's actually not true, well,
then we're not going to trigger the function. Now,
the only problem here is that this user is undefined.
We were only able to pass in the function,
but what if we needed to pass in some arguments associated with that function?
Notice here that all function create blog posts, which is what we want to add.
The decorator to accepts an input in the form of the user. Object.
If I decorate this function with the is authenticated decorator,
and I tried to run this,
then it's not really gonna work because that user is not defined.
And it tells us that the wrapper function takes zero positional arguments,
but one was given.
And the one that's given is this user right here,
this era might already be a good hint for us to figure out how to fix this.
One of the things that we talked about in previous lessons is the concept of OGs
and clogs, unlimited positional arguments.
Normally all add it in as asterix,
OGs and unlimited keyword arguments are usually added in as two Astrix and then
Crocs. So now we can have all function,
which is decorated with this decorate to function.
And when we call the function,
we could provide some inputs and those inputs can be tapped into via these OGs
and Crocs. Now, in this case,
this is a positional argument and it's the argument App positions zero.
So instead of using this user, which is what we would do,
if we were inside this function, instead,
we would use the wrappers OGs,
and then we would use the position set to zero.
So now it's going to look at the function that is going to be passed in which is
going to have some inputs.
And it's going to take that first position input and then see if it is logged in
property is equal to true.
Now that my wrapper is taking positional arguments and keyword arguments,
and I can tap into the first positional argument by using Oxy zero.
Well inside the rapt function,
I can now also use this first argument in order to give it as an input to that
function.
Remember that this function is simply this create blog post function without the
decorator. Now, if I go ahead and hit run,
you will see that nothing happens unless I change this users is logged in
property to equal. True. And now that that's true,
then it's going to be checked right here.
And then it's going to call the function because that user is now logged in this
idea of being able to pass in positional arguments,
as well as keyword arguments,
by simply adding the odds and quarks to the wrapper function,
and then using it inside the wrapper or using it to call the function itself is
one step higher in terms of understanding Python,
decorator functions.
I recommend spending a few minutes looking at this code,
which you can access at this URL, which is in the course resources,
and then trying to recreate this functionality from scratch based on your
understanding of what we just talked about in the next lesson,
I've got a coding exercise for you so that you can try out this new advanced
form of Python decorators and try creating one yourself.