Re: Lambda binding solved?

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Tue, 1 Mar 1994 17:46:00 -0500

On Feb 26, 21:36, Tim Peters wrote:
>
> Assumption: That you want Python to continue using static scoping (more
> than OK by me!).
>

The problem is that is often doesn't LOOK like static scoping.
[ I was about to insist that it ISN'T static scoping, but I see
that the fact that functions carry their module global's environment
around with them makes it static scoping. Maybe it's a hybrid? ]

Here is another example similar to lambda. [ Yes, I know the "right"
was to do it - pass the value of "n" to __init__() and create an
instance variable: self.n ] Yes, this is not the "right" way to
do this in Python, but the problem is that it LOOKS reasonable to
anyone who isn't intimate with the scoping rules.

Python 1.0.1 (Feb 19 1994)
Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>> def makeinc( n ):
... class Incr:
... def incr(self,x) : return x + n
... return Incr().incr
...
>>> a = makeinc( 4 )
>>> a( 3 )
Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 3
NameError: n
>>> n = 1
>>> a( 3 )
4
>>>

Like the lambda problem, this does not necessarily have to be
interpreted as a requirement for nested scope. In both cases,
what I *wanted* was the VALUE of 'n', not the name, but there
was no way ( that I could think of! ) to NOT defer evaluation
of 'n' and use it's value.

But, I am at a loss about whether there is a simple and clean
*general* solution. Mostly I agree with you:

>
> So what do people really want? I just want the current lambda to "work".
>

i.e. lambda is the only thing here that I would really call "broken".
Even the above example may be be reasonable to explain away with:
"You can't do THAT".

So, other than support for nested namespace, I see three sorts of
"fixes" ( in quotes, because 2 of them are really "work-arounds" ):

(1) propagate the local namespace ( perhaps by copy, so that
"lambda x: n = n + x", if allowed, would still do nothing
( although "lambda x: n.append( x )" probably would. )

(2) value(n) or constant(n) - they only look like functions !
( so I guess that could be "value n" without the parends),
they are statements that force evaluation of the current
value of n. This would still confuse folks with code that
LOOKS like it ought to work, but would at least give them
a simple alternative way to do it.

(3) Or some builtin functions sort of like my bind and merge
that explicitly manipulate the functions environment.

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics