2 ms·
You can of course define single line lambdas inline, and I think those are quite attractive, but when you want to get multiline lambdas you get in quite a stick
by Jach 16y ago
You can of course define single line lambdas inline, and I think those are quite attractive, but when you want to get multiline lambdas you get in quite a sticky situation with Python. For a simple example, see the top answer here: http://stackoverflow.com/questions/1233448/no-multiline-lambda-in-python-why-not http://stackoverflow.com/questions/1233448/no-multiline-lamb...
In order to make it work Python would have to introduce a scope-blocking structure like braces, parens, or an 'endlambda', all of which I think are steps in the wrong direction, especially given Python's great feature of semantic whitespace. I'll take a clean function defined above over braces.
I do know of a way to hack a multi-line Lambda with existing syntax, though, but I have no guarantees about it. I don't think I've used it in serious code, since it's just real ugly.
k = lambda x: eval(compile("""
print x
y = 4
print x + y
x = 8
print x
""", '', 'exec'))
x = 5
k(2)
# 2
# 6
# 8
print x
# 5