Advanced Lesson 4
Python Decorators
Chapter 2: Python decorators
Decorators
Now we are ready to talk about decorators.
Take a look at the example below. Can you understand what it is doing?
Test it out yourself to see whether your guess is correct!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Above is an example of a Python decorator.
- A Python decorator is a function (Line 1).
- It takes another function as input (Line 1).
- It wraps this function and adds more functionality to the function (yes, try saying three functions in one sentence!) (Lines 3-5)
- It returns the function (Line 6)
- It reassigns the returned function to the name of the original function (Line 16)
Hopefully it all makes sense!