Suppose a decorator changes the behavior of a function to_lower()
:
def transition(fn):
def to_upper(text: str):
print('the original behavior:' + fn(text))
return text.upper()
def to_capital(text: str):
print('the original behavior:' + fn(text))
return text.capitalize()
return to_upper
@transition
def to_lower(text: str):
return text.lower()
print(to_lower('AaBb'))
>>>
the original behavior:aabb
AABB
This works fine, and if we change the return statement of transition
from return to_upper
to return to_capital
, it changes the behavior from to_lower
to to_capital
, which makes AaBb
to Aabb
Instead of manually modifying the return
of the decorator,
can we modify the decorator with sort of parameter like mode
, if we call
@transition(mode='to_upper')
, it works as return to_upper
and when we call
@transition(mode='to_capital')
, it works as return to_capital
for the decorator?