Python's Call-Time Default Arguments: Flexibility Versus Grammar and Governance Trade-Offs

Python evaluates default argument expressions once, when a def statement executes, and stores the resulting object references on the function. Every later call that omits the argument reuses that stored object, which can cause shared mutable defaults to accumulate state across independent calls. The same early binding freezes dynamic values such as timestamps or environment variables at definition time, and it cannot reference call-time arguments like another parameter's value. PEP 671 proposes late-bound default syntax for Python 3.17 to address these cases by evaluating defaults inside the call frame. Developers currently work around these limitations with sentinel values such as None and in-body initialization.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in