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