Sub topic 1
Parameters and arguments: the vocabulary
Python's documentation, error messages, and most discussions use two words that sound alike: parameter and argument. This chapter fixes what each one means, shows where each appears, and gives you a precise way to describe a call. Later chapters on defaults, keyword-only parameters, and *args build on this vocabulary.
Prerequisites
This chapter assumes you can already:
- Define and call a function in Python, including
return. - Recognize the built-in types: numbers, strings, lists, dicts, and
None. - Read a simple traceback and find the line and exception type.
You do not need to know about default values or keyword arguments yet. This chapter introduces only enough to name them.
The core distinction
A parameter is a name in a function definition. It is a placeholder for a value the function will receive.
An argument is a value in a function call. It is the actual object you hand to the function.
def area(width, height): # width and height are parameters
return width * height
result = area(3, 4) # 3 and 4 are argumentsA short rule: parameters belong to the def, and arguments belong to the call. Some texts call parameters "formal parameters" and arguments "actual parameters". You will see those terms, but "parameter" and "argument" are standard in the Python documentation.
The two sets rarely match one to one in the source code. One function has one list of parameters, but it can be called from many places with different arguments.
print(area(3, 4)) # 12
print(area(10, 2)) # 20
print(area(1.5, 2)) # 3.0What happens at a call
When Python runs a call, it does these steps in order:
- It evaluates the function expression (
area). - It evaluates each argument expression, left to right.
- It binds each argument object to a parameter name.
- It runs the function body with those names as local variables.
Step 2 matters when arguments have side effects. This example makes the order visible.
def show(a, b):
return (a, b)
def tag(label, value):
print("evaluating", label)
return value
print(show(tag("first", 1), tag("second", 2)))
# evaluating first
# evaluating second
# (1, 2)Both arguments are fully evaluated before show starts. The function never sees the expressions, only the resulting objects.
Binding means names, not copies
Step 3 is called binding. Each parameter becomes a local name that refers to the same object the caller passed. Python does not copy the object.
This has two consequences. If the function changes a mutable object in place, the caller sees the change. If the function rebinds the parameter name to a new object, the caller does not.
def append_one(values):
values.append(1) # mutates the caller's list
values = [99] # rebinds the local name only
return values
data = []
print(append_one(data)) # [99]
print(data) # [1]The list data was changed by append, because values and data named the same list at that moment. The assignment values = [99] pointed the local name at a new list and left data alone. Some people call this "pass by object reference" or "pass by assignment". The label matters less than the rule: a call works like an assignment of each argument to its parameter.
This is the root of the mutable default bug covered later in this tutorial. Keep the model in mind: parameters are names, arguments are objects, and calls bind one to the other.
Two ways to write an argument
At the call site, an argument is written in one of two forms.
- A positional argument is matched to a parameter by its position in the call.
- A keyword argument is written as
name=valueand matched to a parameter by name.
def greet(name, greeting):
return f"{greeting}, {name}!"
print(greet("Ada", "Hello")) # positional
print(greet(name="Ada", greeting="Hello")) # keyword
print(greet("Ada", greeting="Hello")) # mixed
print(greet(greeting="Hello", name="Ada")) # keyword order is freeAll four calls print the same text, Hello, Ada!. With keywords, the order of arguments no longer has to match the order of parameters. In a mixed call, positional arguments must come first. Writing greet(name="Ada", "Hello") is a SyntaxError.
The next chapter covers these call forms in detail. Here, the point is vocabulary: "keyword" describes how an argument is written in a call, and it is not a kind of parameter.
Parameters have kinds
Parameters differ in how a caller may supply them. The inspect module reports five kinds. You can list them for any function.
import inspect
def f(a, /, b, *args, c, **kwargs):
pass
for name, p in inspect.signature(f).parameters.items():
print(name, p.kind.name)
# a POSITIONAL_ONLY
# b POSITIONAL_OR_KEYWORD
# args VAR_POSITIONAL
# c KEYWORD_ONLY
# kwargs VAR_KEYWORD| Kind | How it is written | How a caller may supply it |
|---|---|---|
| Positional-only | before `/` | by position only |
| Positional-or-keyword | a plain name | by position or by keyword |
| Var-positional | `*args` | extra positional arguments collect here |
| Keyword-only | after `*` or `*args` | by keyword only |
| Var-keyword | `**kwargs` | extra keyword arguments collect here |
Most parameters you write are positional-or-keyword, so most functions accept both call styles. The other four kinds exist to restrict or widen the interface, and later chapters take them one at a time.
A parameter may also have a default value, written name=value in the def. A parameter with a default is still the same kind. The default only makes the argument optional.
def greet(name, greeting="Hello"): # greeting has a default
return f"{greeting}, {name}!"
print(greet("Ada")) # Hello, Ada!Notice the similar spelling. In def greet(name, greeting="Hello"), the = marks a default. In greet(greeting="Hi"), the = marks a keyword argument. The syntax looks the same, but one sits in a definition and the other in a call. People often mix these up, and the confusion is a common source of muddled explanations.
Terms you may see mixed up
| Phrase | Usually means | Better wording |
|---|---|---|
| "keyword parameter" | a parameter that has a default | "parameter with a default" |
| "optional argument" | a parameter with a default | "parameter with a default" |
| "named argument" | a keyword argument | "keyword argument" |
| "kwargs" | the dict collected by `**kwargs` | "the `**kwargs` parameter" |
"Keyword-only parameter" is a correct term, but it means something specific: a parameter that can only be filled by a keyword argument. It does not mean "has a default". A keyword-only parameter can be required.
Where the vocabulary shows up in errors
Python's TypeError messages describe binding failures. Reading them is easier once the vocabulary is clear.
def area(width, height):
return width * height
area(3)
# TypeError: area() missing 1 required positional argument: 'height'
area(3, 4, 5)
# TypeError: area() takes 2 positional arguments but 3 were given
area(3, depth=4)
# TypeError: area() got an unexpected keyword argument 'depth'
area(3, width=3)
# TypeError: area() got multiple values for argument 'width'The last case happens because the positional 3 already bound to width, and the keyword argument then tried to bind width a second time.
Edge cases worth knowing
- The same function, different call shapes.
area(3, 4),area(3, height=4), andarea(height=4, width=3)all bind the same two parameters. The function body cannot tell which form was used. - Parameters are local. Rebinding
widthinsideareahas no effect outside it. - Arguments can be any expression.
area(len("abc"), 2 + 2)binds the objects3and4. - Lambdas and methods have parameters too. In
lambda x, y=1: x + y,xandyare parameters. In a method, the first parameter (conventionallyself) receives the instance as an argument that Python supplies for you. A call likeobj.method(1)passes two arguments in total, and the error messages count them that way. - A function has one parameter list, but many call sites. When you change a parameter name, every caller using it as a keyword argument breaks. That is why parameter names are part of a function's public interface, a theme returned to in the last chapter.
Summary
- A parameter is a name in a
def. An argument is a value in a call. - A call evaluates arguments left to right, then binds each argument object to a parameter name. Nothing is copied.
- Arguments are written positionally or as
name=valuekeywords. - Parameters come in five kinds, and any of them may be given a default if the rules allow it.
- Binding failures raise
TypeError, and the message names the parameter that went wrong.
Exercise
In the call connect("db.local", timeout=5) to def connect(host, port=5432, *, timeout=10):, name every parameter and every argument, and say which parameter kinds and which argument forms are involved.
Show answer
The parameters are host (positional-or-keyword, no default), port (positional-or-keyword, default 5432), and timeout (keyword-only, default 10). The arguments are "db.local", a positional argument bound to host, and 5, a keyword argument bound to timeout. No argument is given for port, so it keeps its default of 5432.
Exercise
What does this code print, and why? def change(items, extra): items.append(extra) items = ["new"] return items box = ["x"] result = change(box, "y") print(result, box)
Show answer
It prints ['new'] ['x', 'y']. The parameter items is bound to the same list as box, so append changes the caller's list. The assignment items = ["new"] rebinds only the local name, so result is the new list and box keeps the appended value.