Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to Decorators | Decorators
Intermediate Python: Arguments, Scopes and Decorators

Introduction to DecoratorsIntroduction to 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. It might be challenging to grasp at first. Are you ready to tackle these challenges? That's why you're here!

Decorators are functions that add extra functionality to another function, utilizing closures. Here is an example of how a decorator works:

In this example, the decorator() function takes another function as an argument, defines a wrapper() function, encloses the given function within wrapper(), and returns wrapper(). There are three steps to a decorator's operation:

  1. Taking a function as an argument.
  2. Enclosing that function within a newly defined function (wrapper).
  3. Returning the wrapper function, which now encloses the original function.

The wrapper() function contains the main logic of the decorator and invokes the function with the given parameters. The add() function is reassigned by the wrapper() function returned by decorator(), which now contains the enclosed add() function.

  1. The Python interpreter first reads and defines the decorator function. It doesn't execute any code inside the decorator at this point; it just makes a note that there's a function named decorator.
  2. Next, it defines the add function. Like decorator, this function is also not executed yet, just defined.
  3. After defining add, the next line add = decorator(add) is executed. This is where the decorator function is first called. It receives the add function as its argument. Inside the decorator, it defines the wrapper and then returns it. So now, add no longer refers to the original add function but to the wrapper function returned by the decorator.
  4. When add(14, 12) is executed, it's calling the wrapper function with 14 and 12.
  • wrapper prints "Function starts executing"
  • Calls the original add function with 14 and 12, which prints "Function add: 14 + 12" and returns 26.
  • Prints "Function ends executing".
  • wrapper returns 26, which is printed by print(add(14, 12)).

The next calls repeat the previous steps.

Tudo estava claro?

Seção 5. Capítulo 1
course content

Conteúdo do Curso

Intermediate Python: Arguments, Scopes and Decorators

Introduction to DecoratorsIntroduction to 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. It might be challenging to grasp at first. Are you ready to tackle these challenges? That's why you're here!

Decorators are functions that add extra functionality to another function, utilizing closures. Here is an example of how a decorator works:

In this example, the decorator() function takes another function as an argument, defines a wrapper() function, encloses the given function within wrapper(), and returns wrapper(). There are three steps to a decorator's operation:

  1. Taking a function as an argument.
  2. Enclosing that function within a newly defined function (wrapper).
  3. Returning the wrapper function, which now encloses the original function.

The wrapper() function contains the main logic of the decorator and invokes the function with the given parameters. The add() function is reassigned by the wrapper() function returned by decorator(), which now contains the enclosed add() function.

  1. The Python interpreter first reads and defines the decorator function. It doesn't execute any code inside the decorator at this point; it just makes a note that there's a function named decorator.
  2. Next, it defines the add function. Like decorator, this function is also not executed yet, just defined.
  3. After defining add, the next line add = decorator(add) is executed. This is where the decorator function is first called. It receives the add function as its argument. Inside the decorator, it defines the wrapper and then returns it. So now, add no longer refers to the original add function but to the wrapper function returned by the decorator.
  4. When add(14, 12) is executed, it's calling the wrapper function with 14 and 12.
  • wrapper prints "Function starts executing"
  • Calls the original add function with 14 and 12, which prints "Function add: 14 + 12" and returns 26.
  • Prints "Function ends executing".
  • wrapper returns 26, which is printed by print(add(14, 12)).

The next calls repeat the previous steps.

Tudo estava claro?

Seção 5. Capítulo 1
some-alt