If there’s one thing that makes Python incredibly successful, that would be its readability. Everything else hinges on that: if code is unreadable, it’s hard to maintain. It’s also not beginner-friendly then — a novice getting boggled by unreadable code won’t attempt writing its own one day.
Python was already readable and beginner-friendly before decorators came around. But as the language started getting used for more and more things, Python developers felt the need for more and more features, without cluttering the landscape and making code unreadable.
Decorators are a prime-time example of a perfectly implemented feature. It does take a while to wrap your head around, but it’s worth it. As you start using them, you’ll notice how they don’t overcomplicate things and make your code neat and snazzy.
Before anything else: higher-order functions
In a nutshell, decorators are a neat way to handle higher-order functions. So let’s look at those first!
Functions returning functions
Say you have one function,
greet() — it greets whatever object you pass it. And let’s say you have another function,
simon() — it inserts “Simon” wherever appropriate. How can we combine the two? Think about it a minute before you look below.
The output is
'Hello, Simon!'. Hope that makes sense to ya!
Of course, we could have just called
greet("Simon"). However, the whole point is that we might want to put “Simon” into many different functions. And if we don’t use “Simon” but something more complicated, we can save a whole lot of lines of code by packing it into a function like
Functions inside other functions
We can also define functions inside other functions. That’s important because decorators will do that, too! Without decorators it looks like this:
respect() returns a function;
respect("yes") returns the congrats function,
respect("brother") (or some other argument instead of
"brother") returns the insult function. To call the functions, enter
respect("brother")(), just like a normal function.
Got it? Then you’re all set for decorators!
The ABC of Python decorators
Functions with an @ symbol
Let’s try a combination of the two previous concepts: a function that takes another function and defines a function. Sounds mind-boggling? Consider this:
The last line ensures that we don’t need to call
roll() will suffice. Do you know what the output of that call is? Try it yourself if you’re unsure!
Now, as a very good alternative, we could insert this right after defining
This does the same, but glues
startstop() at the onset.
Why is that useful? Doesn’t that consume exactly as many lines of code as before?
In this case, yes. But once you’re dealing with slightly more complicated stuff, it gets really useful. For once, you can move all decorators (i.e. the
def startstop() part above) into its own module. That is, you write them into a file called
decorators.py and write something like this into your main file:
In principle, you can do that without using decorators. But this way it makes life easier because you don’t have to deal with nested functions and endless bracket-counting anymore.
You can also nest decorators:
Note that we haven’t defined
exectime() yet, but you’ll see it in the next section. It’s a function that can measure how long a process takes in Python.
This nesting would be equivalent to a line like this:
Bracket counting is starting! Imagine you had five or six of those functions nested inside each other. Wouldn’t the decorator notation be much easier to read than this nested mess?
You can even use decorators on functions that accept arguments. Now imagine a few arguments in the line above and your chaos would be complete. Decorators make it neat and tidy.
Finally, you can even add arguments to your decorators — like
@mydecorator(argument). Yeah, you can do all of this without decorators. But then I wish you a lot of fun understanding your decorator-free code when you re-read it in three weeks…
Applications: where decorators cut the cream
Now that I’ve hopefully convinced you that decorators make your life three times easier, let’s look at some classic examples where decorators are basically indispensable.
Measuring execution time
Let’s say we have a function called
waste time() and we want to know how long it takes. Well, just use a decorator!
A dozen lines of code and we’re done! Plus, you can use
measuretime() on as many functions as you want.
Sometimes you don’t want to execute code immediately but wait a while. That’s where a slow-down decorator comes in handy:
wakeup() makes lets you take a 5-minute break, after which your console reminds you to get back to work.
Testing and debugging
Say you have a whole lot of different functions that you call at different stages, and you’re losing the overview over what’s being called when. With a simple decorator for every function definition, you can bring more clarity. Like so:
This kinda goes without saying. If you’ve defined a function
decorator(), you can just sprinkle
@decorator everywhere in your code. To be honest, I don’t think it gets any simpler than that!
If you have functionalities that should only be accessed if a user is logged in, that’s also fairly easy with decorators. I’ll refer you to the full example for reference, but the principle is quite simple: first, you define a function like
login_required(). Before any function definition that needs logging in, you pop
@login_required. Simple enough, I’d say.
Syntactic sugar — or why Python is so sweet
It’s not like I’m not critical of Python or not using alternative languages where it’s appropriate. But there’s a big allure to Python: it’s so easy to digest, even when you’re not a computer scientist by training and just want to make things work.
If C++ is an orange, then Python is a pineapple: similarly nutritious, but three times sweeter. Decorators are just one factor in the mix.
But I hope you’ve come to see why it’s such a big sweet-factor. Syntactic sugar to add some pleasure to your life! Without health risks, except for having your eyes glued on a screen.