4 ms·
The crappy thing about Python closures is that you have to define the function above where you call it. I think this kind of puts the cart before the horse and
by crizCraig 16y ago
The crappy thing about Python closures is that you have to define the function above where you call it. I think this kind of puts the cart before the horse and messes of readability.
JavaScript, on the other hand, allows you to define the inner function anywhere in the local scope and makes closures much more attractive, IMO.
Changing this may mean big changes to the interpreter and so may not be worth it. I wonder if lambda created closures are any different...probably not.
- Jach 16y agoYou 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