Sub topic 1
Parameters and arguments: the vocabulary
Prerequisites
This chapter assumes you can define and call a simple Python function, work with the basic built-in types (numbers, strings, lists, dicts, None), and read a short traceback. Nothing else is needed.
Two words for two places
People often use "parameter" and "argument" interchangeably. Python's documentation and its error messages do not, so it helps to keep them apart.
- A parameter is a name listed in the function definition. It is a placeholder for a value the function will receive.
- An argument is a value supplied at the call site. It is what fills the placeholder.
def area(width, height): # width and height are parameters
return width * height
print(area(3, 4)) # 3 and 4 are argumentsHere width and height exist whether or not anyone calls area. The values 3 and 4 exist only for that one call.
| Term | Where it appears | Example above |
|---|---|---|
| Parameter | In the `def` line | `width`, `height` |
| Argument | In the call | `3`, `4` |
What binding means
When you call a function, Python binds each argument to a parameter. Inside the function, each parameter is a local name that refers to the argument's object. Matching arguments to parameters is called argument binding.
Binding does not copy the object. The parameter is a second name for the same object. That matters when the object can change:
def add_end(values):
values.append("end")
data = ["start"]
add_end(data)
print(data) # ['start', 'end']The function changed data because values and data refer to the same list. Keep this in mind. It explains the mutable default bug in a later chapter.
Two ways to supply arguments
A call can pass arguments in two styles. Later chapters cover both in detail, so here is only a preview.
- A positional argument is matched to a parameter by its position:
area(3, 4). - A keyword argument is matched by name, written
name=value:area(height=4, width=3).
Both calls bind width to 3 and height to 4. The names of your parameters are therefore part of the function's public interface, because callers may use them as keywords.
Parameters come in several kinds
A parameter is not always a plain name. A definition can also contain these forms, each covered in its own chapter:
- A parameter with a default value, such as
greeting="Hello", which the caller may omit. - Keyword-only parameters, placed after a bare
*. - Positional-only parameters, placed before a
/. *argsand**kwargs, which collect extra arguments.
Keep the definition-side words (parameters) separate from the call-side words (arguments). Those chapters build on that split.
Why the vocabulary matters in errors
Python's TypeError messages use these exact words. If you know them, the messages are easy to read.
def area(width, height):
return width * height
try:
area(3)
except TypeError as error:
print(error)On Python 3.10 and later, this prints area() missing 1 required positional argument: 'height'. The message says an argument is missing, and it names the parameter that was left unfilled. Passing too many arguments gives the reverse: area() takes 2 positional arguments but 3 were given.
Check your understanding
Exercise
In def power(base, exponent): return base ** exponent and the call power(2, exponent=5), which names are parameters and which values are arguments? Which argument is positional and which is a keyword argument?
Show answer
The parameters are base and exponent. The arguments are 2 and 5. The argument 2 is positional and binds to base by its position. The argument 5 is a keyword argument and binds to exponent by name.