I’m learning flask and i want to unpack args in inner function. That’s my code:
def make_bold(func): def inner(*args, **kwargs): return f"<b>{func(args[0])}</b>" return inner @app.route("/username/<user>") @make_bold def username(user): return f"Username: {user}"
I’m getting an arror “tuple index out of range” even if I add next asterix in
return f"<b>{func(*args[0])}</b>"
Can I unpack it anywhere else?
Advertisement
Answer
If you print args
and kwargs
, you will find that user
is passed in kwargs
(by calling func(user=xxx)
). The problem is that you usually cannot assume how the arguments are passed, so you should write return f"<b>{func(*args, **kwargs)}</b>"
instead.