If theres one thing that makes Python incredibly successful, that would be its readability.
Everything else hinges on that: if code is unreadable, its hard to maintain.
Its also not beginner-friendly then a novice getting boggled by unreadable code wont attempt writing its own one day.

Python was already readable and beginner-friendly before decorators came around.
Decorators area prime-time example of a perfectly implemented feature.
It does take a while to wrap your head around, but its worth it.

So lets look at those first!
It’s free, every week, in your inbox.
Functions returning functions
Say you have one function,greet() it greets whatever object you pass it.

And lets 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 calledgreet(“Simon”).

However, the whole point is that we might want to put Simon into many different functions.
Functions inside other functions
We can also define functions inside other functions.
Thats important because decorators will do that, too!

Then youre all set for decorators!
Do you know what the output of that call is?
Try it yourself if youre unsure!

Added flexibility
Why is that useful?
Doesnt that consume exactly as many lines of code as before?
In this case, yes.

But once youre dealing with slightly more complicated stuff, it gets really useful.
For once, you could move all decorators (i.e.
thedef startstop()part above) into its own module.

Its 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.

Wouldnt the decorator notation be much easier to read than this nested mess?
it’s possible for you to even use decorators on functions thataccept arguments.
Now imagine a few arguments in the line above and your chaos would be complete.

Decorators make it neat and tidy.
Finally, you could even add argumentsto your decorators like@mydecorator(argument).
Yeah, it’s possible for you to do all of this without decorators.

Well, just use a decorator!
A dozen lines of code and were done!
Plus, you’re able to usemeasuretime()on as many functions as you want.
Slowing code down
Sometimes you dont want to execute code immediately but wait a while.
With a simple decorator for every function definition, you’ve got the option to bring more clarity.
Like so:
There is a more elaborate examplehere.
Note, though, that to understand that example, youll have to checkhow todecorate functions with arguments.
Still, its worth the read!
Reusing code
This kinda goes without saying.
To be honest, I dont think it gets any simpler than that!
Before any function definition that needs logging in, you pop@login_required.
Simple enough, Id say.
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 youve come to see why its 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.
This article was written byAri Jouryand was originally published onTowards Data Science.
you could read ithere.