Re: collecting attributes - what's wrong ? lambda binding ?

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Wed, 23 Feb 1994 16:01:14 -0500

After NUMEROUS tries, *THIS* forces evaluation of the local value of
"attrib" into the lambda definition:

def collect2g( seq, attrib ):
ldict = { 'attrib':attrib }
fun = eval( 'lambda obj: getattr( obj, attrib )', ldict, ldict )
return map( fun, seq )

I also vaguely recall some remarks from Guido that lambda was
originally a function taking a string-expression, and that the
new statement syntax was "syntactic sugar". That would tend to
fit the behavious I've observed - I'll have to look up that thread
again.

Beware that Python's "lambda" does not work quite like Lisp "lambda" !

Maybe it needs an optional dictionary argument - except I don't know
how to fit that into the syntax.

[ Warning: pure speculation follows: ]

My current ***guess*** is that lambda compiles the right hand part of the
lambda expression:
lambda_fun = compile( expr_string, '', 'exec' )

but actually returns a function that binds the arguments into a
local dictionary, before executing and returning the value of the
expression:
exec( lambda_fun, __main__.__dict__, constructed_local_dict )

And since the local dict is being used to pass args, no local symbols
are available.

[ I haven't managed to track down all of the code to figure out what
it DOES do. Functions and objects are easy, as they are all very
localized in the python's code. Statements are a bit tougher. ]

Am I in the right ballpark ?

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