Theoretical question about Lambda

Bengt Richter bokr at oz.net
Sat May 4 19:56:25 EDT 2002


On Thu, 02 May 2002 21:05:45 GMT, Alex Martelli <aleax at aleax.it> wrote:
>
>I can't argue that lambda lets you do anything substantial that def
>doesn't -- there would be just no basis to substain such an argument.
>I'm curious to see what you think there might be.
>
I'm working on it ;-) "Substantial" does present an added hurdle ;-)

In the meantime, to clarify the semantics, I wonder whether this
is the truly desired behavior of lambda:

 >>> k='a'
 >>> fa = lambda x:x+k
 >>> k='b'
 >>> fb = (lambda y: lambda x:x+y)(k)
 >>> k='c'
 >>> fa('fa->')
 'fa->c'
 >>> fb('fb->')
 'fb->b'
 >>> dis.dis(fa)
           0 SET_LINENO               1
           3 LOAD_FAST                0 (x)
           6 LOAD_GLOBAL              1 (k)
           9 BINARY_ADD
          10 RETURN_VALUE
 >>> dis.dis(fb)
           0 SET_LINENO               1
           3 LOAD_FAST                0 (x)
           6 LOAD_DEREF               0 (y)
           9 BINARY_ADD
          10 RETURN_VALUE
 >>> `fa.func_closure`
 'None'
 >>> `fb.func_closure`
 '(<cell at 0x007DAE70: str object at 0x007B70D0>,)'
 >>> hex(id('b'))
 '0x7b70d0'

I.e., what is the rationale for not creating a closure when a global
binding exists at the time of lambda evaluation?

Gotta go a few days. May have to catch up via archives. Dang fast-cycle usenet server...

Regards,
Bengt Richter



More information about the Python-list mailing list