W WolfCode · CSC 141

Default Args Greeting

默认参数与关键字参数

≈ 20 min · python-functions · Open in WolfCode

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 valuesport=8080, timeout=30
  • Backwards compatibility — adding a parameter with a default doesn't break existing callers
  • Optional flagsdef fetch(url, retry=False):