r/Python • u/JanEric1 • 19h ago
Discussion PEP 671 – Syntax for late-bound function argument defaults
https://peps.python.org/pep-0671/
The PEP was originally proposed in October 2021 but didnt get much traction. Through some discussions on the python discuss the author picked it up again and reproposed it for 3.17
It proposes a new syntax argument => late_bound_default()
def add_item(item, target=>[]):
def bisect_right(a, x, lo=0, hi=>len(a), *, key=None):for late binding of argument defaults. This would allow avoiding the common mutable default argument trap and would allow for defaults to refer to previous arguments as seen in the biset_right example above.
Discussion can be found here: https://discuss.python.org/t/pep-671-syntax-for-late-bound-function-argument-defaults/110297
Note: I am not the PEP author
16
u/aes110 18h ago
I don't mind the mutable default example, but the bisect one doesn't sit right with me
Running all sort of expressions inside function arguments seems wrong, though i admit it looks kinda cool lol
27
u/HugeCannoli 19h ago
why not the walrus, it's the perfect choice for this behavior.
8
u/def__eq__ 13h ago
I’ll probably be unpopular here, but “just because you can, doesn’t mean you should”.
This opens a Pandora’s box of debug issues and it will end up being used in really obscure ways. Not to mentioned that the simple alternative is to set the default value to None and then in the function decide what the default should be for that parameter. Where’s the issue?
2
u/ohnoitssobig 13h ago
I do not think it will make it. You can implement a decorator and achieve the same behavior but more explicitly and with well-defined boundaries. Changes to typing, introspection are too intrusive. The "left-to-right" evaluation order has to be memorized and is a sort of an arbitrary choice.
15
u/knobbyknee 11h ago
I'm against this change. It would add yet another complexity to the language to support a very niche use case.
It becomes something that everyone has to learn, like all language features, because when you encounter it in someone elses code, you need to understand it.
There are many such burdens in Python these days. Some of them are small, like the walrus operator and some of them are large like async, full understanding of multi-level generators.
8
u/Individual-Flow9158 17h ago
Feels like a code smell to me.
4
u/Chroiche 17h ago
Shared instance for defaults is one of my least favourite things about Python so I welcome it. I'd probably never use the "old" way by choice if this got released as suggested.
2
u/logophage 13h ago
Late binding would definitely be useful and not just for types but also, say, by calling a init fn. I found it useful enough I wrote a set of decorators that lets me do late binding. But having in language would be better.
That said, I don't like
=>only because it's a new form introduced into the language. Someone here suggested:=which kinda works. I'd be inclined to use that.3
u/baubleglue 13h ago
it used to be a simple language
2
u/RevolutionaryFunny40 19h ago
oh nice i’ve always hated the “default” be logic in code because of the default argument trap
R has the same semantics as this, like it a lot.
•
u/OhYourFuckingGod 52m ago
Python's going to end up as a tremendously slow C++ if we keep adding syntax variations every version.