Re: PEP 671 (late-bound arg defaults), next round of discussion!
On Sun, Dec 05, 2021 at 11:09:47AM -0800, Christopher Barker wrote:
But beyond a certain level of complexity, it shouldn’t be in signature anyway. And it can only be a single expression anyway.
True. But in that case, this PEP doesn't help either. Despite what Chris might think, I like this PEP (in principle, if not in detail) but it does have to be said, it's not exactly useful in many circumstances. You need a default value that is generated at every call, not just once when the function is defined, and it has to be limited to a single expression. If the expression is too complex, you will probably put it in its own function; if it is simple enough, you won't need to test it or introspect it.
Does anyone test lambdas in this way? I suspect that’s vanishingly rare.
*waves hand* Sometimes. But not often. The way lambdas are used, as transient functions that get used and discarded, you don't have an opportunity to test them. You pass a lambda in as a key function or callback, it does its job, it gets garbage collected. There's nothing that lasts long enough to test. And usually it is small enough to be obviously correct. For the exceptions, there are times where I will generate a lambda and store it specifically so I can test it, or more likely, re-write it as a def function. In any case, this is not really analogous to default expressions. The typical lambda is transient, but the default expressions we're talking about will belong to long-lived functions which can be tested. The expression has to be equally as long-lived, so it too can be tested when needed. -- Steve
participants (1)
-
Steven D'Aprano