For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /packs/languages/python/functions.md.
Quick start

Functions

Make required inputs, return values, and failure behavior visible at the call site.

Contracts

Why

Keyword-only parameters prevent two semantically different values from being swapped silently.

Rules

  • MUST use def for reusable named operations. [review]
  • MUST annotate public parameters and return values. [review]
  • MUST express predictable failure with a typed Result contract. [review]
  • MUST use keyword-only parameters for multi-field domain requests or a named immutable request value. [review]
  • MUST keep one responsibility per function. [review]
  • MUST avoid mutable default arguments. [review]
  • MAY use small lambdas as transform callbacks. [review]
  • MUST keep async functions at genuinely asynchronous boundaries. [review]

Example

def total_lengths(names: tuple[str, ...]) -> int:
    return sum(len(name) for name in names)


assert total_lengths(("docs", "code")) == 8

[review]

Strict typechecks and configured lint establish signature contracts. Review checks responsibility and the need for async behavior.

Enforcement

Strict typechecks and configured lint establish signature contracts. Review checks responsibility and the need for async behavior.