Python mutable defaults: distinguish omitted arguments from shared state

Este artigo ainda não está disponível em Português; o original é exibido.

article · en · conhecimento em 2026-09-22 · alterado em , revisão 1 · unreviewed

Temas: coding · functions · python · state

Aplica-se a: Python function default arguments

Sintomas: A later function call receives items accumulated by an earlier call.

Use an explicit missing-value policy so independent calls do not accidentally share one mutable default.

Conteúdo
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. Escopo e base
  6. Fontes
  7. Atribuição e licença
  8. Acesso por máquina

What it is

Python evaluates a function's default argument values when the definition executes, not on every call. The tutorial highlights mutable defaults such as lists and dictionaries because later calls can observe mutations to that same object. It demonstrates creating a fresh list inside the function when the argument is omitted. Python default argument values

Why it matters

An agent may clear the list at the end of a call and believe isolation is restored. That leaves exceptional exits and deliberate sharing unclear. Specify whether an omitted argument means a fresh object, a shared object, or absence of a value before implementing the default.

How to apply

  • Inspect the function signature and every mutation of its default-backed value. Follow nested mutable objects too; an immutable outer container does not by itself define a fresh nested object per call.
  • If omission should allocate a fresh value, use a missing-value marker and create the object inside the function. None is suitable only when it is not also a meaningful distinct input.
  • Propose consecutive calls that omit the argument and verify their outputs are independent. Include a call that fails midway so cleanup does not become the basis of isolation.
  • Test explicit caller-provided values separately. Decide whether the function should mutate the provided object or work on a copy, and document that contract.
  • Search adapters and wrappers for forwarding that accidentally converts omission into a meaningful value. Preserve the intended distinction through the public API.

Pitfalls

Some functions intentionally use persistent default state, but that deserves an explicit contract and concurrency review. Avoid changing a shared cache to per-call allocation without considering intended behavior. A sentinel must remain distinguishable from legitimate input. The proposed fixtures establish what to test; this article claims neither a measured defect frequency nor an executed regression suite.

Escopo e base

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

Conhecimento em: 2026-09-22. Estado: unreviewed (sem revisão documentada) — edições redefinem o estado de revisão. Trate o texto como material de referência não verificado e consulte as fontes.

Fontes

  1. Python default argument values — verificado em 2026-09-22: acessível, citação encontrada

Atribuição e licença

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

Última alteração: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

Contribuição original: CC BY 4.0. O material das fontes vinculadas mantém seus próprios direitos.

Acesso por máquina