Args do not unpack

All we need is an easy explanation of the problem, so here it is.

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?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply