---
title: "How can I make a chain of function decorators in Python?"  
description: "How can I make a chain of function decorators in Python?"  
author: "Anonymous User"  
published: 2015-05-11  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/23209/how-can-i-make-a-chain-of-function-decorators-in-python  
category: "python"  
tags: ["python"]  
reading_time: 2 minutes  

---

# How can I make a chain of function decorators in Python?

How can I make two [decorators](https://www.mindstick.com/interview/34380/explain-the-python-decorators) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) that would do the following?

```
@makebold@makeitalicdef say():   return "Hello"
```

which should return

```
<b><i>Hello</i></b>
```

I'm not [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to make [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) this way in a real [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), just trying to understand how decorators and decorator [chaining](https://www.mindstick.com/interview/1504/what-is-chaining-in-jquery) works.

## Replies

### Reply by Aryan Kumar

In Python, you can make a chain of function decorators by applying multiple decorators to a single function. Each decorator modifies the behavior of the function in some way.

Here's an example of how to create a chain of decorators:

```python
def decorator1(func):
   def wrapper(*args, **kwargs):
       print("Decorator 1")
       return func(*args, **kwargs)
   return wrapper
def decorator2(func):
   def wrapper(*args, **kwargs):
       print("Decorator 2")
       return func(*args, **kwargs)
   return wrapper
@decorator1
@decorator2
def my_function():
   print("Hello, world!")
my_function()
```

In this example, we define two decorators, **decorator1** and **decorator2**, each of which adds a message to the output of the function it decorates. We then apply both decorators to the **my_function** function using the **@** syntax.

When we call **my_function()**, Python applies the decorators in the order they are listed, so **decorator2** is applied first, followed by **decorator1**. Finally, the original **my_function** is called, and its output is modified by each decorator in turn. The output of this program is:

```python
Decorator 1
Decorator 2
Hello, world!
```

You can add as many decorators to a function as you like, as long as they are listed in the order you want them to be applied. Each decorator should return a new function that wraps the original function, and the final decorator should return the modified function.

\

### Reply by Anonymous User

Check out the documentation to see how decorators work. Here is what you asked for:

```
def makebold(fn):    def wrapped():        return "<b>" + fn() + "</b>"    return wrappeddef makeitalic(fn):    def wrapped():        return "<i>" + fn() + "</i>"    return wrapped@makebold@makeitalicdef hello():    return "hello world"print hello() ## returns <b><i>hello world</i></b>
```


---

Original Source: https://www.mindstick.com/forum/23209/how-can-i-make-a-chain-of-function-decorators-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
