Python revisits call-time defaults in revived PEP 671
Chris Angelico's five-year-old proposal is back, with core developers split on syntax, evaluation scope, and whether a second form of default is worth the cost.
Python core developers are again weighing syntax for function defaults computed when a function is called, not when it is defined. PEP 671, first drafted five years ago by Chris Angelico, would let authors write a default such as hi=>len(a), so the upper bound of a search is taken from the list actually passed in rather than fixed at definition time.
The revival comes a week after the steering council approved null coalescing with ??, and the two ideas collided immediately. Angelico's draft uses =>. Several contributors suggested tying the feature to ??= instead. Guido van Rossum warned that this must not copy JavaScript, where a missing-value sentinel retriggers the default. Under the proposal, as with today's =, the expression runs only when the caller omits the argument. Passing None is a real value. Paul Moore noted the typing consequence: a parameter written list => [] is an optional list, not list | None, so a caller who passes None is a type error and fails at runtime. That matches what many authors already intend when they default to None and replace it in the body.
The harder open question is what a late-bound expression can see. The motivating case, a default that calls len on another parameter, works only if a default can read earlier arguments. Van Rossum asked whether the expression should instead run in the enclosing scope, the way early-bound defaults do, and what happens if defaults depend on each other in a circle. Angelico said a circular dependency should be an error, and that evaluating in the outer scope would require a closure and a performance cost. Peter Gerlagh pointed at a case where an outer name and a later parameter share a name, and where the current text lets implementations disagree on the result. "I think the PEP should choose here," van Rossum wrote.
David Hagen argued for a sharper change: make late binding the only rule and drop a second syntax. A scan of a large internal codebase found mutable defaults that were never mutated, and none meant to persist across calls. Angelico rejected changing the meaning of existing = defaults. Early binding still has legitimate uses, he wrote, and a vast body of code depends on today's behavior. Gerlagh cited SciPy as code that would break. Others, including Bénédikt Tran, said the feature adds signature complexity for a problem sentinels already solve. Angelico has a reference implementation that passes the test suite, with what he called minor hacks, and said the PEP will at least discuss ??=. No decision has been taken.