5 ms·
Am I the only one who wants multi-lined anonymous functions in Python? I find myself really wanting to reach for arrow functions sometimes while writing Python,
by heavyset_go 6y ago
Am I the only one who wants multi-lined anonymous functions in Python? I find myself really wanting to reach for arrow functions sometimes while writing Python, and end up disappointed that they aren't available.
- fabatka 6y agoI think the basic idea is "if you need multiple lines,you should declare a proper fuction", so I wouldn't stand on one foot until multiline anonymous functions in python.
- trashburger 6y agoI believe Guido was against it as he is mostly opposed to the functional style, as a matter of fact he was opposed to lambas but begrudgingly added then after many requests.
- pansa2 6y agoGuido’s original plan for Python 3 included removing lambda (along with reduce, filter and map) from the language. https://www.artima.com/weblogs/viewpost.jsp?thread=98196 https://www.artima.com/weblogs/viewpost.jsp?thread=98196
- Too 6y ago"Guido was against multi line lambdas" is always brought up in these discussions and then someone digs up an old email from 10 years ago where it happens to be mentioned in passing by. Given the recent success of anonymous functions and it's widespread use and impact in other languages, like Javascript, C#, Java, C++11, maybe it's time to re-evaluate that opinion. I mean, what would javascript be today without promises and multi-line anonymous functions?
- pansa2 6y agoNow that Guido is no longer BDFL nor on the Steering Committee, it doesn’t really matter what he thinks. But, it’s still unlikely that we’ll see full anonymous functions in Python. They just don’t work with its significant-indentation syntax.
- divbzero 6y agoI dug up that “old email from 10 years ago” which I hadn’t seen before. Guido laid out the challenges of multiline lambdas on a mailing list [1] and then followed up with a blog post [2] [3]. His chain of thought is worth reading in full, but the crux is lack of “Pythonicity” and his gut feel that named functions avoid the complexity and possible ambiguity of multiline lambdas: def callback(x, y): print x print y a = foo(callback) [1]: https://mail.python.org/pipermail/python-dev/2006-February/060621.html https://mail.python.org/pipermail/python-dev/2006-February/0... "[Python-Dev] Let's just keep lambda" [2]: https://www.artima.com/weblogs/viewpost.jsp?thread=147358 https://www.artima.com/weblogs/viewpost.jsp?thread=147358 "Language Design Is Not Just Solving Puzzles" [3]: https://news.ycombinator.com/item?id=20672739 https://news.ycombinator.com/item?id=20672739 "Language Design Is Not Just Solving Puzzles (2006) | Hacker News"
- heavyset_go 6y agoThanks for putting this together.
- quietbritishjim 6y agoWhat's wrong with using a nested named functions instead? You may already be aware, but not everyone is: they capture variables from outer scopes in exactly the same way that lambdas do.
- heavyset_go 6y agoIt's just more pleasant to be able to write anonymous functions and to be able to extend them past a single line. If I had to name all of my anonymous functions that I write in other languages, I'd find another way to accomplish what I was trying to do with them.
- netheril96 6y agoIt's wrong because naming is hard. When writing inline, it is possible that not having a name does not impact readability. When defining the function out of line, naming it casually may confuse readers.
- bhargav 6y agoWhen we use lambdas, don't we usual end up assigning them to variables? In those cases, your point about naming will still hold!
- silveraxe93 6y agoLambdas are assigned to function arguments/parameters. Pretty sure assigning a lambda to a variable is an anti-pattern.
- dragonwriter 6y ago> Am I the only one who wants multi-lined anonymous functions in Python? Lots of people want them (lots of people don't, too), but no one has come up with a great syntax that plays nice with the rest of Python and saves you much over named functions.
- mantap 6y agoHey Pythonistas x('.b', onclick=func ev: print("clicked") end) You're welcome.
- pansa2 6y ago`end`? In my Python?!
- mantap 6y agoIt's more likely than you think. You need some end delimiter, it doesn't matter what it is as long as there is one. Just like list literals end with ] and dict literals end with } and str literals end with " or ' etc.
- zild3d 6y ago> it doesn't matter what it is as long as there is one of course it matters, there are design decisions in languages. a certain pattern or syntax may feel just right in one language but very much wrong in another. Throwing in an `end)` like that feels wrong in python https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- mantap 6y agoIt doesn't matter functionally. You can make the end token "waffleiron" or "mariahcarey" or "%^*~$". But there does need to be an end token to make multiline lambdas work and recognizing that is the first step to solving the problem. Once you have the scaffolding of the syntax you can turn it over to the bikeshedders on the mailing list to make it pretty.
- cttet 6y agoYou can do it if you really want to in 3.8 :p : def begin(*args): return args[-1] begin( func := lambda x, y: begin( z := int(input()), x + y + z ), func(1, 2) )