5 ms·
Yeah, Python would really benefit from multi-line anonymous functions. There was a limitation in Python where spacing and indentation gets ignored between pare
by heavyset_go 6y ago
Yeah, Python would really benefit from multi-line anonymous functions.
There was a limitation in Python where spacing and indentation gets ignored between parentheses, which makes it impossible to pass a multi-line lambda as an argument to a method or function. However, given the new parser, that limitation might be able to be mitigated.
- tutfbhuf 6y agoBut you can define functions inside of functions (recursively). def function1(): print ("hello outer") def function2(): print ("hello inner") function2() function1() Output: hello outer hello inner
- throwaway894345 6y agoBut you can’t do that (or try/except) in an expression such as a list comprehension or similar.
- robertlagrant 6y agoYes, because gross.
- deleted 6y ago[deleted]
- dragonwriter 6y agoNor, IMO, should you want to.
- throwaway894345 6y agoWhy not? Many other languages have closures and even Python has lambdas. Multi-line closures work well for these languages including in map/reduce/etc or comprehensions where applicable. Seems like your preference is overfit to Python’s limitations.
- robertlagrant 6y agoBecause you can do it in a function. It's not a limitation when you can do it. Having to define a function that's easier to read, and testable as an independent unit, is not a problem.
- pansa2 6y agoI don’t think this limitation is likely to change - the only ways to allow whitespace-sensitive statements inside an argument list are all really ugly.
- fwip 6y agoYou can do multiple lines, just not multiple statements: >>> f = lambda x: x * \ ... 20 >>> >>> f <function <lambda> at 0x7f8b69f2c6a8> >>> f(2) 40
- deleted 6y ago[deleted]
- pansa2 6y agoIn fact, no statements at all - just a single expression.
- Iwan-Zotow 6y agowell, you could pack a lot of other expressions into that expression, especially with 3.8/3.9, e.g. x,y = 1,2 q = list(map(lambda t: (tx := t*x, ty := t*y, tx+ty)[-1], [1, 2, 3]))
- pansa2 6y agoWalrus operator: not even once.
- Iwan-Zotow 6y agowalrus, actually, being an expression, it's a perfect fit for lambdas
- scott_russell 6y agoAgreed. I suspect that most uses of python decorators become moot with proper multi-line anonymous functions. I assume some would argue that decorators create more readable code but they seemed like a syntax hack to me.
- mumblemumble 6y agoI would argue that multi-line anonymous functions are unpythonic. Exhibit A is this line from PEP 20: Readability counts. I'm currently about a month into learning a legacy codebase that was written in a functional language. If I could single out one thoroughly egregious practice that has made this code far, far more difficult to read and understand than it should have been, it's multi-line anonymous functions. In general, if a function is doing something complicated enough to need multiple lines of code, it's doing something complicated enough to merit an explicit name.
- deleted 6y ago[deleted]
- uryga 6y ago> if a function is doing something complicated enough to need multiple lines of code, it's doing something complicated enough to merit an explicit name. def foo_and_bar(x): foo(x) bar(x) whew! good thing i named that IME this limitation just leads to throwaway names like process(x) go(x) do_foo(x) # in foo(x) cb/callback/fn because a lot of things just don't have sensible names! just like how it'd suck to have to come up with a name for every loop body (although there was some book advocating for "replace every loop body with a named function" so some people enjoy that i guess...)
- mumblemumble 6y agoI see you chopping the first two words off that quote. ;) In this specific case, that single instance of a single pattern is such a throw-away that it doesn't deserve a name, but the pattern itself is easy enough to name. So I'd skip the single-purpose function and create a combinator. def do_each(*args): def helper(x): for fn in args: fn(x) return helper and then, when I need to do both foo and bar, I don't even need a lambda. map(do_each(foo, bar), some_sequence) That's a fairly specific case, though. Moving back to the general, I would say that a function that does more than one thing, but can't easily be named, is a code smell. Of course, every general rule has its exceptions. But I'm not so keen on the idea of optimizing one's coding style for the exceptional cases. Going back to PEP 20, "Special cases aren't special enough to break the rules." (I realize mapping a function that returns nothing is terrible, but I'm feeling too lazy to think of a better example.)
- tda 6y agoIf you really want though, you can write a web server in a single python lambda: (lambda flask: (lambda app: (app, app.route('/')( lambda: 'Hello World!' ) )[0] )(flask.Flask('__name__')).run() )(__import__('flask')) See https://gist.github.com/e000/1023982 https://gist.github.com/e000/1023982 for more horrible examples
- heavyset_go 6y agoI hate it, but it is clever.