>> Let's talk a little bit about Decorators.
Now, the goal of this series is to help get you up to speed,
help teach you enough Python.
So that way, you can go off and start
learning how to use things like COG services,
how to create web applications etc.
Basically, our hope is
that when you've finished all of these videos,
that you'll have enough information that you could
start reading through some docs,
reading through some quickstarts and go, "Oh, okay.
I understand what's going on here."
So as part of that,
one of the big things that I wanted to call out is
this little Python concept known as a Decorator.
Now, I am going to show you how you could create a Decorator.
But to be honest with you,
chances are you're not going to be
creating a whole lot of Decorators on your own.
The reason that I'm showing you how to
create one is just so that way you can see
a little bit of behind the scenes of
how these work and how these operate.
Instead, the most common way that you're going to interact with
Decorators is by using Decorators as part of your code.
So if you look through the "How to
create" and you just sort of go, "Well, all right.
But I don't necessarily see
a lot of places where I'm going to use that."
That's just fine.
The big thing really is in using
Decorators that are part of a framework such as Flask,
that you're going to be creating your application using.
So with that, let's talk a little bit about Decorators.
Now, to do that, I actually want to take a small step back here
and talk about some programming components
or some programming concepts.
In particular, I want to talk about
Objects and then Functions or Methods.
Objects are nouns, they're data constructs.
They're the what?
They're the things that we're going to operate upon.
We also then have our functions and our methods.
There are verbs, there are methods, there are actions.
These are all the different things
that we're going to be able to do.
So for example, my OS you might consider that an object,
my get env, that's going to be our verb.
That's something that we can do.
We can go get environmental variable.
Our Decorators then, are like the adjectives.
They allow us to describe our code.
They allow us to add additional meaning to our code.
So that way, if we're running it inside of another environment,
maybe we're running it inside of Django or
we're running it inside of Flask.
That Flask knows what it is that we're trying to tell
it or when it should call different things that we've created.
Decorators are used most commonly,
to be able to add additional functionality to your code.
Let's take, for example,
this little snippet from Flask.
Flask is a web programming framework.
So what we're going to do with Flask
is we're going to create a web application.
Now, at the end of the day when we're
creating a web application with Python,
what we're really doing is we're allowing people to call methods,
to call functions that we've created.
The way that they're going to do that is by
specifying some particular URL.
So if you're thinking about maybe a public API or you go
to some site.com forward
slash products and then maybe some product name,
and you're curious, "Hey,
how is this then knowing that it's supposed
to load up the details of a particular product?"
Well, the way that it knows that is
because something has been registered that says, "Hey,
when a user navigates to this URL,
this is the method or the function that I want you to then call."
So what I want to do is I want to register this.
So that way when somebody navigates to
my server/api/products that we're
going to then call whatever is inside of that function.
It could then be code to go call database or whatever.
The code itself is really not the important part.
The important part is right up here. The important part.
Let me clean off all the ink on
my slide here and let's highlight the important part.
The important part is right here,
that little route registration.
What I want you to notice with that route registration
is that API/products. Hey, wait a minute.
That matches this API/products right here.
So what I'm doing with
this little route Decorator is I'm now telling Flask,
"Hey, when somebody goes to API/products,
this right here, this is the code that I want you to call."
So that is what's going to make the magic happen.
That is a Decorator.
Now, you might decide at some point
that you want to create a Decorator on
your own or maybe you just curious,
"Hey, how does this work behind the scenes?"
This is what it might look like behind the scenes.
So what we've got up top is we've got our Decorator,
and the name of my Decorator is logger.
You'll notice down below,
that I'm going to call Logger the
exact same way that we had previously.
So instead of using route instead,
I'm going to call logger.
Now, what we need to do is we need to tell Python,
"Hey, if somebody uses our Decorator,
we want you to call a particular function."
That's this wrapper function right
here and we're going to return this back.
So this is my code.
I'm going to print out two little messages and then in the middle,
I'm going to go ahead and call your code.
So the eventual results here when this code executes is
we're going to get this output
that we're going to see that logging execution,
we're going to see the code that was inside sample function,
and then we're going to see "Done logging".
That is how we create a Decorator.
Now, I do again want to stress here that
it's not very often that
you're going to find yourself creating a Decorator.
So if you're looking at this and you're going, "Hey,
you know I'm not really sure when I would ever do this."
That's okay.
The most common time that you'd be
creating a Decorator on your own
is if you are going to be creating your own framework.
There is some power here.
So for example, if I need to log execution
which is what I'm trying to demo here or
maybe there's something where I need to do some level of
authentication and I need to make sure that the users log on,
then I might go ahead and create a Decorator.
But if you think about it,
those are relatively esoteric.
If you also think about it,
chances are somebody else has already created a package for you.
So if you're looking at this
and you're going, "Hey, you know what?
I'm really not sure what's going on there."
That's actually just fine.
Because again, the most common interaction that you're going to
have with Decorators is
when you use one that somebody else has already created,
which is what we're seeing right here.
So what I'm going to do from here is I'm actually going to do
a real quick demo on how you might create a Decorator,
how you might then call that Decorator that you've created.
So that way, we can see that little bit of behind the scenes.
In case you did want to go in and play with it or
you were going to create an application
that was going to use them.
But again, and I know I've said this before,
but I just want to highlight it one last time,
the most commonplace that you're going to interact with
Decorators is when you're going to
use them like you've seen up here.
Let's get in and take a look at a demo.