
On Wed, Oct 27, 2021 at 3:39 AM Erik Demaine <edemaine@mit.edu> wrote:
On Tue, 26 Oct 2021, Steven D'Aprano wrote:
def func(x=x, y=>x) # or func(x=x, @y=x)
This makes me think of a "real" use-case for assigning all early-bound defaults before late-bound defaults: consider using closure hacks (my main use of early-bound defaults) together with late-bound defaults, as in
``` for i in range(n): def func(arg := expensive(i), i = i): ... ```
I think it's pretty common to put closure hacks at the end, so they don't get in the way of the caller. (The intent is that the caller never specifies those arguments.) But then it'd be nice to be able to use those variables in the late-bound defaults.
I can't say this is beautiful code, but it is an application and would probably be convenient.
Got any realistic examples? Seems very hackish to me.
Perhaps most natural is to add a new introspection object, say LateDefault, that can take place as a default value (but can't be used as an early-bound default?), and has a __code__ attribute.
Yeah, I'll have to play with this. It may be necessary to wrap *both* types of default such that you can distinguish them. Effectively, instead of a tuple of values, you'd have a tuple of defaults, each one stating whether it's a value or a code block. ChrisA