Multiline lamba implementation in python.
jgilbert.python at gmail.com
Tue Jun 12 05:02:37 CEST 2007
I don't expect multiline lambdas to be added to Python. I'm not so sure that
that's a bad thing. Regardless, isn't it possible to write your own
implementation of multiline lambdas as functions? Wouldn't that be a win-win
Anyway, the meat of the idea is this:
>>> def myLambda(args, body):
fun = 'def f(' + args + '):' + body
... ... ... ...
>>> funWithNoName = myLambda('x', '''
... ... >>>
>>> [myLambda('x', '''\n print x\n return(x**2)''')(x) for x in range(3)]
[0, 1, 4]
There are a few tricks with the indenting, no question. myLambda should be
more intelligent than it is, I'm thinking of using regular expressions to do
simple indentation checking. Lexical variable scoping would be tricky, but
that's not one of a python's strong suits.
What am I missing?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list