Multiline lamba implementation in python.

Josh Gilbert jgilbert.python at gmail.com
Mon Jun 11 23:02:37 EDT 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
for everyone?

Anyway, the meat of the idea is this:
>>> def myLambda(args, body):
    fun = 'def f(' + args + '):' + body
    exec fun
    return f
... ... ... ...
>>> funWithNoName = myLambda('x', '''
  print x
  return(x**2)''')
... ... >>>
>>> [myLambda('x', '''\n  print x\n  return(x**2)''')(x) for x in range(3)]
0
1
2
[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?


Josh.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070611/ccc3eee5/attachment.html>


More information about the Python-list mailing list