4 ms·
You can nest lambdas to get the equivalent of several expressions. I once wrote a Runge-Kutta example on Rosetta Code showing this: http://rosettacode.org/wik
by grondilu 10y ago
You can nest lambdas to get the equivalent of several expressions. I once wrote a Runge-Kutta example on Rosetta Code showing this:
http://rosettacode.org/wiki/Runge-Kutta_method#using_lambda http://rosettacode.org/wiki/Runge-Kutta_method#using_lambda
It does not look as bad as one might expect, though the nesting of parenthesis makes things messy.
- masklinn 10y agoYou can but you still can't get statements in there.
- ergl 10y agoYou can hack together multiple expressions chaining them with and http://sigusr2.net/one-line-echo-server-using-let-python.html http://sigusr2.net/one-line-echo-server-using-let-python.htm...
- zardeh 10y agoThat still doesn't get you statements. You can't get context managers or exception handling (although you can raise exceptions) into lambdas, I've tried.
- masklinn 10y agoWell you might be able to if you add a bunch of named function combinators wrapping these, but definitely not with only lambdas, unless you define your combinators using `ast`, which I think would let you define statements via expressions.
- AnkhMorporkian 10y agoThat's an awful lot of effort to go to to avoid naming a function.
- zardeh 10y agoWell sure, you could do something like def apply_ctx(ctx, func, *func_args, **func_kwargs): with ctx as __ctx: func(*func_args, **func_kwargs, ctx=__ctx) but you can't do that itself as a lambda. And I consider modifying the ast cheating :P
- masklinn 10y ago> but you can't do that itself as a lambda. And I consider modifying the ast cheating :P No disagreement, really depends whether you're a "rules" or "spirit" kind of person though.