Sub topic 3
Giving parameters default values
This chapter assumes you can define and call a function, know the basic built-in types (numbers, strings, lists, dicts, None), and can read a simple traceback.
What a default value is
A default value is a value written in the def line with =. Python uses it when the caller does not supply an argument for that parameter. A parameter with a default is optional. A parameter without one is required.
def greet(name, greeting="Hello", punctuation="!"):
return f"{greeting}, {name}{punctuation}"
print(greet("Ada")) # Hello, Ada!
print(greet("Ada", "Hi")) # Hi, Ada!
print(greet("Ada", punctuation="?")) # Hello, Ada?
print(greet(name="Ada", greeting="Hey")) # Hey, Ada!Here name is required. greeting and punctuation are optional.
How Python fills in the values
When you call a function, Python matches each argument to a parameter. It then checks every parameter that received nothing. If that parameter has a default, Python uses the default. If it has none, Python raises a TypeError.
Skipping defaults with keywords
In the third call above, greet("Ada", punctuation="?") leaves greeting at its default and sets only punctuation. Without keyword arguments you would have to write greet("Ada", "Hello", "?") and repeat the default yourself. Keywords let you override one default without touching the ones before it.
You can also pass a required parameter by keyword, as in greet(name="Ada", greeting="Hey"). Keyword arguments may appear in any order, but positional arguments must come first in the call.
Choosing good defaults
A default should be the value most callers would pick anyway. Common choices are:
- A number such as
0,10, or a standard port. - A short string such as
"utf-8". - A boolean flag such as
verbose=False. None, meaning "the caller gave nothing".
Defaults let you add options to a function later without breaking existing calls. If greet once took only name, adding greeting="Hello" keeps every old call working.
Ordering and timing
Two rules about defaults get their own chapters, so this chapter only previews them.
- Python rejects a required parameter placed after an optional one in a plain definition. It reports a
SyntaxError. - Python evaluates the default expression once, when the
defstatement runs. It does not re-evaluate it on each call. This is harmless for numbers and strings. It causes a well-known bug with lists and dicts.
A common mistake with required parameters
Beginners sometimes give every parameter a default just to avoid errors. This hides mistakes. If name had a default of "", then greet() would return ", !" instead of failing. Leave a parameter without a default when no sensible default exists. The TypeError is then useful feedback.
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
greet() # TypeError: greet() missing 1 required positional argument: 'name'Exercise
Given def box(width, height=1, fill="#"), which of these calls raises a TypeError, and what does box(3, fill="*") use for height?
Show answer
box() raises a TypeError because the required parameter width is missing. The other calls are valid. In box(3, fill="*"), width is 3, fill is "*", and height takes its default value of 1.