-: Hi guys and welcome back.
Let's look at the at syntax for decorators.
Here we've got our code from the last video.
We have our make secure function,
that defines an inner function,
that will be used to replace,
the function that we want to make secure.
This function checks the access level,
and then returns a call to the original one,
such as the get admin password function.
Now I'll have you know that instead of doing this
get admin password equal make secure of get admin password,
which can have some problems,
for example if you decide to call get admin password
before that line runs you can use the at syntax,
which does exactly the same thing.
That is at make secure,
if you put at make secure in front of or on top of
a function definition that will prevent the function
from being created as is and instead it will create it
and pass it through the decorator, in one go.
Though you no longer need that here
and now you'll see that if we run this code
you will get no admin permissions for Jose.
Because at the moment I am a guest,
but if I change this to an admin,
then it will all work out.
However there is a small problem with decorators as a whole.
Both when you assign them
and also when you use the at syntax.
Which is that when you replace this function,
by this one here,
the name of the function changes.
The get admin password it still exists
as a name inside Python
but it is no longer registered as a function internally.
Now the secure function is registered
as a function internally,
and there are some libraries that will
actually use that internal name.
So, for example if you print get admin password dot name
you'll see what I mean.
The name of get admin password, is actually secure function.
So, that is a bit of a problem,
a problem that we have to solve.
In addition, if you have any documentation attached
to get admin password it will also be lost
when you replace it with secure function,
because this one doesn't have any documentation.
To fix that problem all you have to do
is actually use another decorator.
So, will go into import functools,
which is short for function tools,
this is a built in module in Python,
and then we're going to decorate our secure function.
We're gonna say at functools dot wraps
and then we're going to tell secure function
that it is a wrapper for func.
And therefore what this does is essentially
it keeps the name and the documentation
of this function if there are any.
So the name is always gonna be there
if we now run this code you can see
that get admin password is what comes out.
So that's what the functools wraps is for
you need that on pretty much every decorator you write,
so do get accustomed to using that.
Remember, you wanna put it on top of
the inner secure function
not on top of this make secure decorator.
Some terminology at this point by the way
the make secure function is called a decorator
because you can use it decorate a function.
This here is not a decorator,
that's just a function that will replace your other one.
All right, that's everything for this video.
I wanted to show you the at syntax for decorators
and also functools wraps those two are essential
when you're working with decorators in Python.
Thanks for joining me in this one,
hope you've learned something,
and I'll see you in the next video.