Conteúdo do Curso
Mastering Python: Closures and Decorators
Mastering Python: Closures and Decorators
What are Decorators?
Let's discuss decorators.
Note that "Decorators" can be a challenging topic for beginners. We will examine decorators in detail, step by step, explaining how they work, but it may be challenging to comprehend. Are you prepared for the challenges? That's why you're here!
Note
The prerequisite topic for understanding decorators is closure. Therefore, make sure you understand how closure works before diving into decorators.
Decorators
Decorators are functions that add extra functionality to another function using the closure.
def decorator(func): def wrapper(argument1, argument2): print("Function starts executing") result = func(argument1, argument2) print("Function ends executing") return result return wrapper def add(a, b): print(f"Function add: {a} + {b}") return a + b add = decorator(add) print(add(14, 12)) print(add(11, 28)) print(add(33, 16))
Here is an example of how a decorator works. The decorator()
function takes a function as an argument, defines the wrapper()
function, encloses the taken function within the wrapper()
, and returns the wrapper()
. There are three steps to the decorator's work:
- Taking a function as an argument.
- Enclosing the function within the newly defined function (
wrapper
). - Returning the
wrapper
function with the enclosed function.
The wrapper()
function contains the main decorator logic and invokes the function with the given parameters.
The add()
function is reassigned by the returned wrapper()
function that now contains the enclosed add()
function.
Decorator Implementing
Step 1. Define the decorator.
The decorator should take exactly one argument.
Step 2. Define the inner function.
We need to define the inner function to close the function taken by decorator()
.
Step 3. Enclose the taken function.
The function should be called inside the inner function (wrapper
), and the result should be saved and returned.
Step 4. Return the inner function.
The decorator should return the inner function wrapper
without calling.
How does the decorator work?
Step 1: Decorator is called.
The decorator()
function is called and takes the function (decorated function) as the argument func
. At this step, the interpreter creates the decorator()
local scope.
Step 2: Define the wrapper function.
The interpreter defines the wrapper()
function (in the decorator()
local scope) that takes the same arguments as the decorated function. The wrapper()
body contains the main logic of the decorator and calls the function from the non-local scope.
The wrapper()
function is not executed at this step.
Step 3: Decorator execution ends.
The decorator()
function ends the execution and returns the wrapper()
function. The interpreter removes the decorator()
local scope but leaves the enclosed objects.
Step 4: Reassignment
The returned wrapper()
function assigns to the decorated function: The decorated function is replaced by the other function (wrapper()
), and the previously contained function is removed.
The previous add()
function is removed but enclosed inside the returned wrapper()
function.
Step 5: Usage
The new function is the returned wrapper()
function that takes arguments and inserts them into the enclosed function.
Note
The
add
is a variable that contains a reference to the function that we defined.
Obrigado pelo seu feedback!