What would PEP 671 change about Python function defaults?

November 27, 2026 4 answers
Asked by 6w+gasketmaraca_439

I keep seeing PEP 671 mentioned as a way to evaluate defaults when a function is called. How would that work, and is it better than using None and setting the default inside the function?

Answers

CricketLedger-2n

The proposal adds a separate syntax for a late-bound default. For example, def add_item(item, target=>[]): would create a new list when target is omitted, rather than reusing the list created when the function was defined.

It can also express a default that depends on another argument, as in def search(a, hi=>len(a)):. The proposed defaults are evaluated at call time, from left to right, so later defaults can use earlier arguments.

Regular defaults would still behave as they do now. This gives you another option, not a change to every existing function.

TinkerScone!4:

So the mutable default trap would still exist with the regular equals syntax?

Reply to this answer
Tavdexo-7c

The None pattern is fine for plenty of functions. The awkward bit is that the signature says None even when the actual default is something else, and you have to look in the body to find it.

It also treats an explicitly passed None like an omitted argument if you check for None. A late-bound default would be used when the argument is omitted, so those are not quite the same behavior.

Reply to this answer
8l~ferretbanjo_362

I like the fresh list example, but defaults that call functions or depend on several other arguments could make a signature harder to read and debug. Having the logic in the body makes the sequence more visible. Whether the shorter signature is worth the extra syntax and teaching is still debatable.

Reply to this answer
2e!inkwellapricot_637

The proposed => syntax is one point of disagreement. Some would prefer reusing :=, but a colon already introduces parameter annotations, which could make that spelling confusing in a signature. Others would rather avoid adding a new syntax altogether.

Decorators are another possible way to express late binding, though that raises its own questions about how clearly the behavior shows up in the function signature and tooling.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions