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

Course Content

Intermediate Python: Arguments, Scopes and Decorators

Chaining DecoratorsChaining Decorators

Chaining decorators in Python is a powerful feature that allows you to apply multiple decorators to a single function. Each decorator modifies or enhances the function in some way, and they are applied in a specific order. Here's a practical example to illustrate chaining decorators:

Two Simple Decorators

First, let's define two simple decorators:

Chaining Decorators

Now, let's apply these decorators to a single function:

In each decorator, there is code that runs before moving on to the next decorator and after the completion of this decorator. It works in a layered manner. They are applied in the order they are listed, from top to bottom. The output of the upper decorator becomes the input for the next one down. This layering can add multiple functionalities to the original function.

Execution Order

When greet() is called, the execution order will be:

  1. From decorator_one the first print is executed;
  2. Inside decorator_one, fromdecorator_two the first print is executed;
  3. Finally, the original greet function is executed;
  4. Then, move backward and finish executing the decorator_two by printing the message;
  5. The whole code is finished by executing the last print statement in the decorator_one.

The output will be:

Chaining decorators is a powerful way to extend the behavior of functions in a modular and readable way.

Hope the next challenge helps you to better understand this topic.

Everything was clear?

Section 5. Chapter 5
course content

Course Content

Intermediate Python: Arguments, Scopes and Decorators

Chaining DecoratorsChaining Decorators

Chaining decorators in Python is a powerful feature that allows you to apply multiple decorators to a single function. Each decorator modifies or enhances the function in some way, and they are applied in a specific order. Here's a practical example to illustrate chaining decorators:

Two Simple Decorators

First, let's define two simple decorators:

Chaining Decorators

Now, let's apply these decorators to a single function:

In each decorator, there is code that runs before moving on to the next decorator and after the completion of this decorator. It works in a layered manner. They are applied in the order they are listed, from top to bottom. The output of the upper decorator becomes the input for the next one down. This layering can add multiple functionalities to the original function.

Execution Order

When greet() is called, the execution order will be:

  1. From decorator_one the first print is executed;
  2. Inside decorator_one, fromdecorator_two the first print is executed;
  3. Finally, the original greet function is executed;
  4. Then, move backward and finish executing the decorator_two by printing the message;
  5. The whole code is finished by executing the last print statement in the decorator_one.

The output will be:

Chaining decorators is a powerful way to extend the behavior of functions in a modular and readable way.

Hope the next challenge helps you to better understand this topic.

Everything was clear?

Section 5. Chapter 5
some-alt