Let me ask you a question.
What happens if this hello function actually took a parameter?
Let's say a string.
Or a greeting.
And a greeting was going to be printed here.
Let's remove the bye for now.
Would this still work if I run?
Hello?
Pause the video and think about it.
Let's find out.
Nope.
That's an error.
If you can try to pause the video here and see if you can solve this, how can we make this allow arguments?
I see that on line five right here.
It says line five.
Hello is missing one required positional argument greeting because when we actually call hello, we're
expecting to have a parameter.
But here we're not giving it any arguments.
How can we solve this?
Well, the one way is to just simply say.
Greedy, right?
Or we can say whatever we want.
We can say X and then this.
Wrapper function receives the parameter.
So that if I click Run.
Hmm.
Line 13 is now giving me an error, because while now we need to actually give it a greeting, so let's
say hi if I click Run.
All right.
This works.
And I hope this makes sense.
Right.
We're calling.
Hello.
With.
Hi.
So this function.
Hello gets past.
We then?
Receive as argument the x right.
Remember underneath the hood my decorator is saying a equals my decorator.
And hello.
Right.
So all we're doing is running my decorator.
Hello?
Which now turns into their rap function.
And when we do the rapt function, A, we give it an argument of high.
So the rapt function accepts this parameter and then runs the function.
Hello.
But here's the question what if a greeting also had an emoji that it accepts?
So we're printing, greeting, and then the emoji.
Hmm.
What do we do?
Well, again, we'd have to create another parameter and then another parameter, and then here, call
it with, let's say, a smiley face.
If I click Run.
Well, this should be a string.
If I click Run.
This works, but this is hectic every time I need to change parameters.
Well, it's I have to modify this.
And what if we had keyword arguments like.
Default.
Hi.
There's actually a pattern here that we can use that makes things really, really simple for us.
And all we do is this we do the star arcs, which we have seen, which takes all position arguments,
and then star star key word arcs, which takes all the keyword arguments and we call the function by.
Singh star arcs to unpack all the positional arguments and star star key word arcs to unpack all the
keyword arguments.
Now, if this looks confusing, don't worry.
This is just a pattern that you can just copy and paste and remember.
But now check this out.
I can do emoji equals to, let's say, sad face.
As a keyword argument.
And now if I just pass.
Hi.
Like this and hit run.
And let's just remove this as well.
Since we don't need it, we'll call hello properly because we have the decorator and then click run.
Look at that.
I get high with the sad face.
Now, the reason this is called a decorator, it's because it's a famous pattern in programming.
It's called the decorator pattern.
And this is the decorator pattern.
It gives our decorator flexibility so that we're able to pass as many arguments as we want into our
rapt function by using star arcs and keyword arcs and then unpacking them like this inside of a function.
And this syntax, let's say if we didn't even have the print statements, this syntax is why decorators
are so powerful.
By just using these lines of code, we're able to add functionality using the decorator pattern to decorate
our functions.
And the decorator patterns are used all over programming.
So it's a very, very powerful concept.
To finish off the lesson on Decorator.
I want to talk to you about some of the ways that decorators can be useful and some of the common locations
that we're going to see decorator patterns in our code and we're going to build our own performance
decorator that's going to be able to tell us how our functions perform, how fast they are.
And I promise you, by the end of that, video decorators are going to make sense to you and you're
going to be able to explain to other people and other friends why we should use decorators in our programs.
I'll see you in that one.
Bye bye.