Search code examples
pythonmultithreadingsyntax-error

How can I solve SyntaxError: positional argument follows keyword argument


I am trying to run this code here:

threads = [threading.Thread(name='ThreadNumber{}'.format(n),target=SB, args(shoe_type,model,variant)) for size in SizeList for n in range(ThreadCount)]

However this is what my terminal is returning:

   threads = [threading.Thread(name='ThreadNumber{}'.format(n),target=SB, args(s
hoe_type,model,variant)) for size in SizeList for n in range(ThreadCount)]
                                                                           ^

SyntaxError: positional argument follows keyword argument

Is there anyway I can fix this?

SB is referring to a function that I am trying to run.


Solution

  • In Python, the order in which we pass (and receive) arguments to a function matters.

    Positional arguments come first, variable-length arguments come next, variable-length keyword arguments come last.

    The expected syntax looks like this:

    function(arg, *args, **kwargs)
    

    The argument names above are only conventional examples, so a real-world function will look like:

    about_user(name, *hobbies, **favorite_foods)
    

    If we call (or receive) with the argument types out of order, we get errors similar to the one you encountered. In your specific case, your keyword arguments are first, when they should be last.

    I wrote a small article about *args and **kwargs that has a few more related details and examples.