Default Args Greeting
默认参数与关键字参数
Parameters can have default values. Callers can skip them — or override only the ones they care about, using keyword arguments.
Defaults make optional parameters
Python · runnable
Keyword arguments — the killer feature
greet("Ada", punctuation=".") leaves greeting
at its default "Hello" but overrides the third parameter.
Without keyword args you'd have to write greet("Ada", "Hello", ".")
— forcing you to remember the default.
When to use defaults
- Frequently-used good values —
port=8080,timeout=30 - Backwards compatibility — adding a parameter with a default doesn't break existing callers
- Optional flags —
def fetch(url, retry=False):