Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Syntax | Decorators
course content

Зміст курсу

Intermediate Python: Arguments, Scopes and Decorators

SyntaxSyntax

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
This code defines a decorator indicate and three functions avg_two, avg_three, and avg_many_kwargs , each decorated with indicate. Here's a brief description of each component:


  1. Decorator indicate(func) function:
  2. - Adds functionality to print arguments and a separator before and after executing a function.
    - 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.
  3. The @indicate decorator is applied to three functions:
  4. - 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.

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

Секція 5. Розділ 2
course content

Зміст курсу

Intermediate Python: Arguments, Scopes and Decorators

SyntaxSyntax

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
This code defines a decorator indicate and three functions avg_two, avg_three, and avg_many_kwargs , each decorated with indicate. Here's a brief description of each component:


  1. Decorator indicate(func) function:
  2. - Adds functionality to print arguments and a separator before and after executing a function.
    - 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.
  3. The @indicate decorator is applied to three functions:
  4. - 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.

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

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