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>"
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>"
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>