Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Flexible Decorators | Decorators
Mastering Python: Closures and Decorators
course content

Зміст курсу

Mastering Python: Closures and Decorators

Mastering Python: Closures and Decorators

1. Scopes
2. Closure
3. Decorators

bookFlexible Decorators

Different functions require a varying number of arguments.

To create a decorator that can be applied to functions with varying numbers of arguments, you can use the *args and **kwargs as arguments in the wrapper() function.

Let's create an indicate decorator that prints information about the function execution:

  1. Print a message that the function has started execution.
  2. Print the arguments taken by the function.
  3. Call the function and save the result that is returned by the function.
  4. Print a message that the function has finished execution.
  5. Return the result.
1234567891011121314151617181920212223242526272829303132
def indicate(func): def wrapper(*args, **kwargs): print("=" * 5,"Func execution starts:", func) print("Taken arguments:", *args, kwargs) result = func(*args, **kwargs) print("=" * 5, "Func execution ends:", func) return result return wrapper @indicate def add_two(a, b): return a + b @indicate def add_three(a, b, c): return a + b + c @indicate def add_many_kwargs(**kwargs): string = "" number = 0 for key, value in kwargs.items(): string += key number += value return (string, number) print("Returned:", add_two(14, 21), "\n") print("Returned:", add_three(225, 12, 11), "\n") print("Returned:", add_many_kwargs(first=51, second=11, third=47, fourth=93))
copy

The indicate() has the wrapper() that takes the *args and **kwargs. That means that wrapper() can take every number of arguments and keyword arguments.

The add_two() function takes 2 arguments.
The add_three() function takes 3 arguments.
The add_many_kwargs() takes a different number of keyword arguments.

The indicate() decorator can be used for every function.

So, the *args and **kwargs in the wrapper() allow us to use the decorator for any function.

Note

Remember to unpack the received args and kwargs using asterisks (* for args and ** for kwargs) when passing them to a nested function.
If you don't understand or have forgotten how to do this, feel free to come back here.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 3
some-alt