Sub topic 4
Passing arguments by position and by keyword
This chapter assumes you can define a function with def, know the difference between a parameter and an argument, and can use return. It covers the two basic ways a caller matches arguments to parameters, and the errors that come from mixing them up.
Two ways to match arguments to parameters
A parameter is the name in the def line. An argument is the value the caller supplies. Python matches them in one of two ways:
- A positional argument is matched by its place in the call. The first argument goes to the first parameter, the second to the second, and so on.
- A keyword argument is matched by name, written as
name=value.
def describe(name, age, city):
return f"{name}, {age}, from {city}"
print(describe("Ada", 36, "London")) # positional
print(describe(name="Ada", age=36, city="London")) # keyword
print(describe(city="London", name="Ada", age=36)) # keyword, any orderAll three calls print the same text. With keywords, the order of the arguments does not matter. This makes calls easier to read, because age=36 says what 36 means.
Mixing the two styles
You can use both styles in one call. Positional arguments must come first. Python fills the leading parameters from the positional values, then matches the keywords to the remaining names.
def power(base, exponent=2):
return base ** exponent
print(power(5)) # 25
print(power(2, 10)) # 1024
print(power(2, exponent=10)) # 1024
print(power(exponent=3, base=2)) # 8Common errors
Most mistakes come from one of the rules below. Each produces a TypeError.
| Mistake | Example call | Why it fails |
|---|---|---|
| Positional after keyword | `power(base=2, 10)` | This is a `SyntaxError`, because positional arguments must come first. |
| Same parameter twice | `power(2, base=3)` | `2` already filled `base`, so `base=3` is a duplicate. |
| Unknown keyword | `power(2, exp=3)` | The function has no parameter named `exp`. |
| Missing argument | `power()` | `base` has no default and no value. |
| Too many arguments | `power(1, 2, 3)` | There are only two parameters. |
The duplicate case is the least obvious. You can see the message by catching the error:
def power(base, exponent=2):
return base ** exponent
try:
power(2, base=3)
except TypeError as error:
print(error) # power() got multiple values for argument 'base'Choosing a style
Positional arguments work well when the meaning is obvious and there are few of them, such as power(2, 10). Keyword arguments work better when:
- The function takes several parameters of the same type, such as three numbers.
- The value is a bare literal like
Trueor0, which says nothing on its own. - You want to skip an optional parameter and set a later one.
def connect(host, port=80, secure=False, timeout=10):
return f"{host}:{port} secure={secure} timeout={timeout}"
print(connect("example.com", timeout=30))
# example.com:80 secure=False timeout=30The call above skips port and secure and sets only timeout. Positional arguments cannot skip a parameter, so the keyword form is the only readable option.
Exercise
Given def area(width, height=1): return width * height, which of these calls raise an error, and why? (a) area(3, 4) (b) area(height=4, 3) (c) area(3, width=4) (d) area(width=3)
Show answer
(b) is a SyntaxError because a positional argument follows a keyword argument. (c) is a TypeError because 3 already fills width, so width=4 gives it two values. (a) returns 12 and (d) returns 3, because height defaults to 1.
Exercise
Write a call to connect that sets secure to True for the host "example.com" and leaves port and timeout at their defaults.
Show answer
connect("example.com", secure=True) returns "example.com:80 secure=True timeout=10".