Contenido del Curso
Intermediate Python: Arguments, Scopes and Decorators
2. Arguments in Function
3. Function as an Argument
Intermediate Python: Arguments, Scopes and Decorators
Syntax
Decorator works by taking a function as an argument and then executing it within a wrapper function.
To apply a decorator, one can use the @ symbol followed by the decorator function's name, placed right above the function that needs to be modified. Here's an example:
In this example, both methods achieve the same outcome. The first method, using the @
symbol, is a more readable and concise way to apply decorators
, often referred to as "syntactic sugar".
It's typical to use a nested function named wrapper
within decorators.
Flexible Decorators: Accommodating Diverse Function Arguments
Functions often require a varying number of arguments.
To develop a decorator capable of being applied to functions with differing argument counts, it's effective to utilize *args
and **kwargs
in the wrapper()
function.
Code Description
indicate
and three functions avg_two, avg_three
, and avg_many_kwargs
, each decorated with indicate
. Here's a brief description of each component:- Decorator
indicate(func)
function:
- Adds functionality to print arguments and a separator before and after executing a function. - The
@indicate
decorator is applied to three functions:
-
-
wrapper
takes arguments *args
and **kwargs
and pass them to the func
call.- The
*args
allows the wrapper()
function to accept any number of positional arguments as a tuple.- The
**kwargs
allows the wrapper()
function to accept any number of keyword arguments as a dictionary.avg_two(a, b)
: Calculates and returns the average of two numbers, displaying additional information due to the decorator.-
avg_three(a, b, c)
: Computes the average of three numbers, with additional prints from the decorator.-
avg_many_kwargs(**kwargs)
: Finds the average of multiple numbers passed as keyword arguments, also showing argument details through the decorator.¿Todo estuvo claro?
Contenido del Curso
Intermediate Python: Arguments, Scopes and Decorators
2. Arguments in Function
3. Function as an Argument
Intermediate Python: Arguments, Scopes and Decorators
Syntax
Decorator works by taking a function as an argument and then executing it within a wrapper function.
To apply a decorator, one can use the @ symbol followed by the decorator function's name, placed right above the function that needs to be modified. Here's an example:
In this example, both methods achieve the same outcome. The first method, using the @
symbol, is a more readable and concise way to apply decorators
, often referred to as "syntactic sugar".
It's typical to use a nested function named wrapper
within decorators.
Flexible Decorators: Accommodating Diverse Function Arguments
Functions often require a varying number of arguments.
To develop a decorator capable of being applied to functions with differing argument counts, it's effective to utilize *args
and **kwargs
in the wrapper()
function.
Code Description
indicate
and three functions avg_two, avg_three
, and avg_many_kwargs
, each decorated with indicate
. Here's a brief description of each component:- Decorator
indicate(func)
function:
- Adds functionality to print arguments and a separator before and after executing a function. - The
@indicate
decorator is applied to three functions:
-
-
wrapper
takes arguments *args
and **kwargs
and pass them to the func
call.- The
*args
allows the wrapper()
function to accept any number of positional arguments as a tuple.- The
**kwargs
allows the wrapper()
function to accept any number of keyword arguments as a dictionary.avg_two(a, b)
: Calculates and returns the average of two numbers, displaying additional information due to the decorator.-
avg_three(a, b, c)
: Computes the average of three numbers, with additional prints from the decorator.-
avg_many_kwargs(**kwargs)
: Finds the average of multiple numbers passed as keyword arguments, also showing argument details through the decorator.¿Todo estuvo claro?