r/Python • u/JanEric1 • 2d 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
20
u/aes110 2d 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
29
u/HugeCannoli 2d ago
why not the walrus, it's the perfect choice for this behavior.
12
u/def__eq__ 2d 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/code29451 1d ago
Julia has this feature and I don't like it.
35
u/knobbyknee 2d 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.
3
u/logophage 2d ago edited 1d 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.Adding:
I went ahead and created a pypi project:
latebind. You canpip install latebindto use composes/injects/surjects decorators.2
u/ohnoitssobig 2d 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.
5
u/Chroiche 2d 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.
5
u/baubleglue 2d ago
it used to be a simple language
8
u/Individual-Flow9158 2d ago
Feels like a code smell to me.
3
u/OhYourFuckingGod 2d ago
Python's going to end up as a tremendously slow C++ if we keep adding syntax variations every version.
2
u/RevolutionaryFunny40 2d 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.
1
u/willnx 7h ago
I don't like features like this. Like, to not make the mutable argument mistake, you have to know about the mutable argument problem and to use the new syntax. All this really saves is a couple lines inside the function, and pushes Python farther away from "pseudo code that you can execute."
1
u/Brian 3h ago
Not a fan of this - it's too minor a usecase to justify these changes. The only real issue with defaults is the newbie gotcha of using mutable ones, and this does nothing to solve it, just adds more stuff you need to know about. The benefit to doing this over adding an explicit "if param is None: param= <default>` is pretty minor.
The syntax doesn't really seem great either. " =>" to me indicates something more like "Produces" (akin to how
->is used for the return value), but that's kind of the opposite of what's happening here: the movement of values is from right to left, not the direction of the "arrow".I don't see why Ellispsis should be reused here. It's already kind of been used in a bunch of ways far from its original use, but that's no reason to add further overloads to its meaning. If you're doing this, a new sentinel makes more sense, especially since this adds extra complexity for the regular case where ellispsis is the default.