lambdak: multi-line lambda implementation in native Python

Yawar Amin yawar.amin at
Fri Jan 16 03:07:43 CET 2015

On Thursday, January 15, 2015 at 1:40:09 AM UTC-5, Ian wrote:
> On Wed, Jan 14, 2015 at 11:06 PM, Steven D'Aprano
> <steve at ...> wrote:
> [...]
> > def func(a, b=None):
> >     global spam
> >     import math
> >     spam = [a, b]*3
> >     print spam
> >     del spam
> >
> > value = [1, "hello", int, func]
> > del func
> [...]
> # Untested, but seems like this should work.
> [...]
> To the OP: I note that although import_ is used in the examples, it's
> not found in the list of functions in the README.

Thanks Ian. I'll update the readme soon. It's still a little behind the
actual code.

Steve, here is your example translated, about as simple as I can make

    from lambdak import *

    value = [
      given_(lambda a, b = None:
        import_("math", lambda math:
        given_(lambda globs = globals(), spam = "spam":

        assign_(spam, [a, b] * 3, globs, lambda:
        print_(get_(spam, globs), lambda:
        del_(spam, globs))))))

    del value[3]

The problem with globals is that a Python module can't easily manipulate
the globals of its caller modules. Here you can see I had to work around
the issue by just creating a few functions[1] which generically work
with dicts and then explicitly passing in the globals dict on each call.
I don't see any other reasonable way to do it. I would welcome being
proven wrong here.

To the responders in the 'beauty of the code' subthread: yes, I realise
that lambdak is not Pythonic, and it will make angels cry, and all that.
My view is you should actually be _happy_ that it looks like this. If
anyone ever asks about multi-line lambdas again, you can point to
lambdak and say, 'See! This is what happens if you try to go against
Pythonic style.' And then you can shrug your head in disgust if they
continue to use it anyway.



[1] `assign_`, `get_`, `del_`. I haven't pushed these to the project
repo yet. Will do so after writing up the tests and docs.

More information about the Python-list mailing list