Home > Back-end >  Args do not unpack
Args do not unpack

Time:04-19

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?

CodePudding user response:

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.

  • Related