'How to chain two decorators

How can I make two decorators in Python that would do the following?

@bold_tag
@italic_tag
def greet():
   return input()

Input: Hello

Output should be

"<b><i>Hello</i></b>"


Solution 1:[1]

They're just regular decorators, nothing to it...

Using functools.wraps makes sure the wrapped function smells like the original.

from functools import wraps


def bold_tag(fn):
    @wraps(fn)
    def wrap(*args, **kwargs):
        return f'<b>{fn(*args, **kwargs)}</b>'

    return wrap


def italic_tag(fn):
    @wraps(fn)
    def wrap(*args, **kwargs):
        return f'<i>{fn(*args, **kwargs)}</i>'

    return wrap


@bold_tag
@italic_tag
def greet(s):
    return f"Hello, {s}!"


print(greet("you"))

The output is

<b><i>Hello, you!</i></b>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AKX