Member-only story
Understanding Python decorators: Six levels of decorators from beginner to expert
How decorators work, when to use them and 6 examples in increasingly complexity
Decorators are a very handy tool that can be used to change the behavior of a function without modifying the function itself. They allow you to easily add functionality without changing existing code. There are many use-cases (some of which we’ll get into today) like logging, performance checking, verifying permissions etc.
After this article you have a clear understanding how decorators work, how to apply them and when to apply them. We’ll start with the easiest, most basic example and then slowly work up our way to more complex ones. In the end we’ll have a function, decorated with multiple decorators of different types. Let’s code!
How decorators work
First we’ll specify a function that we want to decorate:
As you can see this function is called ‘sayhello’ and we can execute it by running sayhello()
…