
Zachary F. answered 07/24/21
Basic Math, General Computer Skill, and Python3 Tutor
def makebold(func):
def wrapper(*args, **kwargs)
return f"<strong>{func(*args, **kwargs)}</strong>"
return wrapper
def makeitalic(func):
def wrapper(*args, **kwargs)
return f"<i>{func(*args, **kwargs)}</i>"
return wrapper
@makebold
@makeitalic
def say():
return "Hello"
====================================
Right now, executing say() would return "<strong><i>Hello</i></strong>"
Decorators are just functions that return a function.
@makeitalic
def say():
return "Hello"
is just syntax sugar for say = makeitalic(say)
When you stack decorators, they are executed in reverse order, so in my example, say finishes executing first, then makeitalic finishes executing second, then finally makebold, execute last.