It's time to talk about decorators.
And now their key term in Python.
And we've actually seen decorators before.
Do you remember when we were building classes?
I taught you that there was a class method and a static method.
And I said, Hey, don't.
Don't worry about this.
We'll talk about this later.
Well, this is now later.
Decorators look like this.
They have the add sign and then some sort of name following it.
But in order for us to fully understand what decorators are, we have to talk about functions in Python
and why they're so powerful.
This is the neat thing about Python in Python.
Functions are what we call first class citizens.
That is, they can be passed around like variables.
They can be an argument inside of a function.
They act just like, well, variables.
Let me show you.
If I create a hello function, which frankly is completely useless.
Because all it does is say hello to.
If I do this.
Remember I said functions are pretty much just variables in Python.
Again, something that is not always the case with all programming languages.
So if I wanted to, I can say greet equals two.
Hello.
So that if I print greet.
And I run this.
I get.
Hello?
What if I just pass?
Hello?
Like this?
If I run this.
Well, I get the function location in memory, so I would have to use grit like this and run it right.
We have to call the hello function.
Okay.
What if we do this?
What if I say delete?
Hello.
A keyword in Python that deletes that function.
What do you think will happen?
Let's see if I run this.
Hmm.
Is that what you expected?
Did you expect Grete to still work?
The interesting thing about Python is that I create hello and this is now created in memory.
When we get to line four, I say, hey, greet is going to point to hello.
So hello can still be called down here.
I can still say hello.
Because hello is the name of the function that points to that location in memory.
But when I do delete hello, all it does is say, hey, I'm going to delete this function, this name
reference to this function that's in memory.
However, because greet a whole nother variable is still pointing to this function.
I'm going to delete the hello word.
So if I go hello like this?
I'm going to get an error name.
Hello is not defined, however.
The greet is still pointing in memory to this location.
So Python is smart enough to say, Hey, you told me to delete hello, I'll delete the name hello.
But I'm not going to delete the function because Grete is still pointing to it.
So functions in Python act just like variables do.
They're first class citizens.
We can also pass functions.
Around inside of arguments, right?
If I say hello and then this hello receives another function that calls, let's say.
Function like this.
Well, I can create a greet function, let's say.
Define greet.
And this function is just going to print.
Still here.
Now in here if I call.
Hello?
With.
Greet.
Or just so we're clear, let's just say a equals two.
Hello, greet.
And if I print a, what will happen?
That still works because what happens is I say, hey, call the function.
Hello?
Och I'm calling the function.
Hello with the argument greet.
So we go here and we say hey run function and what's function.
We want to call this function right because we have these brackets so we jump to what function is which
in our case is great.
So we go over here and say, Hey, just print this.
We didn't have to call it in here because the hello function calls it for us.
So why did I just teach you all of that?
What does that have to do with decorators?
Decorators are only possible because of these features, this ability of functions to act like variables
act like first class citizens in Python.
And when we start learning about decorators underneath the hood, they're using this power of functions.
Now and then upcoming videos.
We're going to talk about how to create our own, what they mean, and expand on this idea of a decorator.
But here's a short form of what decorators do.
Decorators supercharge our function.
If we had our let's say hello function here.
By adding some sort of a decorator like this, we can supercharge our function and add extra functionality
to it.
It lets the python interpreter know I want this hello function to have some extra features.
But let's explore this topic a little bit more in the next video.