Skip to main content

How to Call a Python Function with Keyword Arguments

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A call like make_tea("black", 0, True) works, but nobody can tell what 0 and True mean. Keyword arguments name each value at the call, make_tea("black", milk=True), so the call explains itself and you can skip the parameters whose defaults are fine.

Syntax​

Syntax
<function>(<positional value>, <parameter>=<value>, <parameter>=<value>)
PartWhat it does
<positional value>Matched to the parameters by position, from the left.
<parameter>=<value>A keyword argument: matched to the parameter with that name, in any order.
OrderPositional values first, then keyword arguments.

The Python glossary defines a keyword argument as "an argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **."

The Example​

The example defines make_tea with one required parameter and two with defaults, then calls it three ways.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which defines make_tea(kind, sugar=0, milk=False) returning a description of the tea, and prints make_tea("green"), make_tea("black", milk=True) and make_tea(kind="mint", sugar=2).

main.py
def make_tea(kind, sugar=0, milk=False):
return f"{kind} tea, sugar: {sugar}, milk: {milk}"


print(make_tea("green"))
print(make_tea("black", milk=True))
print(make_tea(kind="mint", sugar=2))
  • make_tea("green") passes only kind; sugar and milk keep their defaults.
  • make_tea("black", milk=True) skips sugar and sets milk by name.
  • make_tea(kind="mint", sugar=2) names every value it passes.

Step 2: Run It​

Terminal
uv run python main.py
Output
green tea, sugar: 0, milk: False
black tea, sugar: 0, milk: True
mint tea, sugar: 2, milk: False

A zsh terminal where uv run python main.py prints green tea with sugar 0 and milk False, black tea with sugar 0 and milk True, and mint tea with sugar 2 and milk False.

Each keyword landed on the parameter it named, and the defaults covered the rest. The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A positional value after a keyword, as in make_tea(kind="mint", 2)SyntaxError: positional argument follows keyword argumentPut positional values first
A keyword the function doesn't have, as in honey=TrueTypeError: make_tea() got an unexpected keyword argument 'honey'Check the parameter names
Passing the same parameter twice, as in make_tea("green", kind="black")TypeError: make_tea() got multiple values for argument 'kind'Pass each parameter once

Troubleshooting​

SymptomLikely causeFix
got an unexpected keyword argumentA misspelled or unsupported keywordCompare it with the def line
got multiple values for argumentThe value was given by position and by nameDrop one of them

FAQ​

Can a function force keyword arguments?​

Yes. Parameters after a * in the def line, as in def make_tea(kind, *, milk=False):, can only be passed by keyword.

Do keyword arguments work with built-in functions?​

With many, such as print("a", "b", sep="-"). Some built-in parameters are positional-only and can't be named.

Conclusion​

Name a value at the call with parameter=value to match it by name, skip the ones whose defaults suit you and make the call readable. Keep positional values before keyword ones.

Additional Resources​