09. Stacking Decorators
Multiple decorators can be applied to a single function.
Example
def bold(func):
def wrapper():
return "<b>" + func() + "</b>"
return wrapper
def italic(func):
def wrapper():
return "<i>" + func() + "</i>"
return wrapper
@bold
@italic
def greet():
return "Hello"
print(greet()) # <b><i>Hello</i></b>
Order Matters
Decorators are applied from the bottom up.
Practice Challenge
Write two decorators uppercase and exclaim and stack them on a function returning "hello".
Wrap-Up
- Multiple decorators can be stacked.
- Application order is important.