Re: Lambda binding solved by lateral thinking?

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Wed, 2 Mar 1994 12:12:54 -0500

This was excatly what I was trying for with my suggestion
of a 'value' or 'constant' statement, but as Tim rightly pointed
out, my suggested syntax was quit ambiguous about where and
when that evaluation was forced. Your syntax neatly avoids
that by putting the binding in the argument list.

Well, not exactly what I was trying for: If you're saying what
I read you as saying, you could also override the default in
the function call:

>>> f = make_incrementor( 4 )
>>> f(1)
5
>>> f(1, incr=1)
2

Or am I misreading you?
( As a fix for the local binding problem, I like it.
I haven't thought thru the implication of the above yet. )

- Steve Majewski

On Mar 1, 23:52, Guido.van.Rossum@cwi.nl wrote:
>
> How about the following piece of lateral thinking: add default
> argument values, and evaluate the default values (once) in the
> defining scope. This solves a totally unrelated problem (*args is
> most often used to implement optional arguments) and can lead to
> relatively clean code using "idiom" like the following:
>
> def make_incrementor(incr):
> return lambda x, incr=incr: return x + incr
>
> or (without lambda):
>
> def make_incrementor(incr):
> def incrementor(x, incr=incr):
> return x + incr
> return incrementor
>
> This should discourage abuse of nested scopes for structuring purposes
> while satisfying the justifiable desire for importation of variables
> from outer scopes into nested functions. (Note that no exportation is
> supported!)
>
> -- End of excerpt from Guido.van.Rossum@cwi.nl