When stacking decorators, they execute bottom to top (innermost first).
1def bold(func):2 def wrapper():3 return f"<b>{func()}</b>"4 return wrapper56def italic(func):7 def wrapper():8 return f"<i>{func()}</i>"9 return wrapper1011def underline(func):12 def wrapper():13 return f"<u>{func()}</u>"14 return wrapper1516@bold # Applied third17@italic # Applied second18@underline # Applied first19def hello():20 return "Hello"2122print(hello())23# <b><i><u>Hello</u></i></b>2425# Equivalent to:26hello = bold(italic(underline(hello)))
Execution order:
underline wraps hello.italic wraps the result.bold wraps the result.bold runs first, then italic, then underline.