Arguments: defaults, *args and **kwargs
When you call a function, Python hands each value you pass to one of its parameters. Follow one function, order(drink, size="S", *extras, **notes), through three calls and watch the values land in its boxes: by position, by name, from a default, and the leftovers packed into a tuple and a dict. Finish with the error you get when a required value is missing, and the two ordering rules Python enforces.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Given def greet(name, greeting="Hi"): print(greeting, name) what does greet("Sam") print?
Only one argument is passed, so it fills name by position and greeting takes its default, "Hi". print() writes greeting first: Hi Sam.
02With def order(drink, size="S", *extras, **notes) what is extras inside order("latte", "L", "oat", "honey")?
drink and size take the first two values; the star in *extras packs every extra positional argument into one tuple: ('oat', 'honey').
03Same function. Inside order(size="L", drink="tea", shot=2) what are drink, size and notes?
Keyword arguments go by name, so their order does not matter: drink gets 'tea' and size gets 'L'. No parameter is called shot, so **notes collects it into a dict: {'shot': 2}.
04Which call to def order(drink, size="S") is a SyntaxError?
In a call, positional arguments must come before keyword arguments. Python refuses order(size="L", "tea") with: positional argument follows keyword argument.
05What happens on order() when drink has no default?
A parameter without a default is required. Python checks the arguments before the body runs and raises a TypeError naming the missing parameter.
Glossary
- Parameter
- A name listed in a def's brackets, such as drink. It receives a value each time the function is called.
- Argument
- A value passed in a call, such as "tea" in order("tea").
- Positional argument
- An argument given without a name. Positional arguments fill parameters from left to right.
- Keyword argument
- An argument given with a name, such as size="L". It goes to the parameter with that name.
- Default value
- A value written in the def, such as size="S". Python works it out once, when def runs, and uses it when a call leaves that parameter out.
- *args
- A parameter with one star. It collects every extra positional argument into a tuple.
- **kwargs
- A parameter with two stars. It collects every extra keyword argument into a dict of name: value pairs.
- Frame
- The fresh set of parameter and local values a function gets each time it is called.
Notes and sources
- Python binds every argument of a call at the same moment, before the body on line 2 runs; the figure shows them one group at a time so each rule can be seen on its own.
- A default is evaluated once, when def runs, and then shared by every call. That is why a list default such as [] keeps growing between calls: use None and make the list inside the function instead.
- args and kwargs are only the usual names; the * and ** do the work. Each call gets a new tuple and a new dict.
- A parameter written after *extras, such as def f(*args, sep=" "), can only be passed by keyword. print() itself works this way.
- Python tutorial: More on defining functions (defaults, keyword arguments, *args, **kwargs)
- Python reference: Function definitions
- Python reference: Calls (how arguments are bound to parameters)
- Python glossary: argument
- Python glossary: parameter
- Python FAQ: Why are default values shared between objects?
- Python library: Built-in types (tuple, dict)
Quiz Unlocks after the last step.