add tag
anoldmaninthesea
I'm trying to understand how to work with keyword matching and positional matching in Python.

For that, I'm using the following function:

```
def f(a,b,*be_a_tuple, **be_a_dict):
	print(a,b,be_a_tuple,be_a_dict)
```

Then I try to run:

```
f(22,a=1,5,6,56,kwnotinheader1=1,kwnotinheader2="Amazing")
```

But I get the following error: 
> positional argument follows keyword argument

When I change to 

```
f(22,1,5,6,a=56,kwnotinheader1=1,kwnotinheader2="Amazing")
```

I get the error 
> f() got multiple values for argument 'a'

Is there a way to call the function, and make variable 'a' to match by keyword, instead of by position?
Top Answer
Pax
If you are allowing a dynamic number of arguments, you can't pass a positional argument as a keyword argument.

In a function call, keyword arguments are required to be listed last. Combine that with `a` being a positional argument. Calling `f(22,1...` means `a` takes the value `22`.

That's why it's saying `f() got multiple values for argument ‘a’`: you passed `22` and then passed another by using the keyword `a=56`.

So function calls like this is OK:

```
>>> func_args(1,2,3)
1 2 (3,)

>>> func_kwargs(a=1,b=2,c=3)
1 2 {'c': 3}
```

But this is not,

```
>>> func_args(a=1,b=2,3)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
```

Where func_args and func_kwargs are defined,

```
def func_args(a,b, *args):
  print(a,b, args)

def func_kwargs(a,b, **kwargs):
  print(a,b, kwargs)
```

Simply put, keyword arguments are required to be listed last.

---

If you want to _require_ parameter `a` to be passed as a keyword argument, you can do something like:

`f(b, *, a, **be_a_dict)`

Still, in this case, you can't have a dynamic number of arguments, although you can have a dynamic number of keyword arguments.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.