Let's finally write our own decorator.
Remember, a decorator supercharges our function.
It's simply a function that wraps another function and enhances it or changes it.
But the best way for me to explain what it is is to actually code our own.
So let's do that.
Let's define a my.
Decorator, and this decorator is going to accept a function right away.
We should know that this is a higher order function now in here.
Inside of here, we're going to call the function.
But before we do that, we're actually going to wrap this function in another function.
So we're going to say define the wrap function.
So this is just a wrapper and this wrap function.
Is going to be a function that simply runs this funk.
And then finally we'll just return wrap function.
And notice here that I'm not calling it, I'm just returning it so that later on somebody can use it.
Okay.
Now, let's say we have a simple function.
Hello.
And this simple function just prints.
Hello.
What a useless function.
Now.
Why is this useful?
Because now I can use this as decorator python.
As soon as we write an A in front of this is going to say, hey, this is going to be a decorator and
the decorator.
As long as we follow this syntax of accepting a function, having a wrapper function, calling the function
and returning the wrapper function can be used.
So if I do my decorator see how it's all blue.
Now this decorator can actually be used on top when defining our functions.
So if we want to use my decorator on the hello function, we use it like this so that when I call hello
next in my program.
I click run.
I get.
Hello?
Okay.
I know what you're thinking.
You're thinking this is a complete waste of time, Andre.
Why did I have to do all of that?
I could have just done this.
Just define the.
Hello.
And that was it.
I'm so confused.
Well, here's the thing.
What we just did with the decorator.
Hasn't changed anything.
It works exactly as expected.
But what it allows us to do is that now we can add extra functionality.
Remember how I said that decorators is a function like my decorator that wraps that is here another
function which is the function that we pass it.
And enhances it.
So how can we enhance?
Hello?
Well, we can do anything inside of this wrapped function.
For example, I can say print, and then we'll just print some stars in here and maybe print.
Another set of stars in here so that when I call run.
Boom.
We've just super boosted our.
Hello.
Function.
How cool is that?
And this is why decorators are useful by just adding this one line.
I'm able to super boost a function so that let's say I create another function called.
Bye.
And this buy is going to print.
See ya later.
By calling by here and clicking run.
Well, nothing changes.
But if I wanted to super boost it, just like the.
Hello.
All I need to do is just copy the decorator and use it here as well.
So that if I run it.
There we go.
We've super boosted our buy.
This is the power of decorators.
By just using this syntax, I can add extra functionality to other functions.
Now I know what you're thinking.
And I know I also keep saying that a lot.
It's like I can read minds, but.
You're thinking this is a lot of magic.
How does Python interpreter do all of this?
It seems like.
Well, magic, right?
Underneath the hood.
All it does.
Well, let me show you.
Based on what we've learned so far about functions, because they're first class citizens.
Right.
All we're doing is literally this.
We're saying variable a.
Equals.
Hello?
And wrapping this hello and calling it with my decorator.
So I'm saying my decorator.
And then after that I am running a like this.
So let's say this was hello to.
Well, I'm just running it.
Like so.
So that if I run this.
Everything still works.
All I'm doing is wrapping my hello function.
With my decorator and assigning it to a variable.
So if we go step by step, we see that my decorator receives a function.
In our case the hello function.
It then returns this wrapped function that has hello in here.
So we return this wrap function.
So hello to now equals this wrapped function and then this wrapped function gets called and what does
it get called with.
Well it prints this, then it runs the hello function and then it prints this.
It's the same as doing something like this.
We simply call it again.
If I run this.
It works.
That looks strange, doesn't it?
But it should make sense.
We're calling the first function, and the first function returns another function, and then we call
the next return function, which is wrap function.
Now the reason decorators are useful is because instead of doing this and frankly looking confusing,
I can just add the at my decorator and we don't need to do all of this.
Our hello gets automatically wrapped by my decorator, but essentially this is what the art scene is
doing.
See?
Nothing magic about it.
Now.
Right now.
This is nice, but I bet you're still wondering why decorators are useful.
So in order to really finalize our understanding of decorators, let's take a break and I'll see you
in the next video.