5 ms·
I don't understand what the use of anonymous functions besides lambdas would be
by aceofspades19 18y ago
I don't understand what the use of anonymous functions besides lambdas would be
- jrockway 18y agoThe use is the same, it's just that the definition can now be more than one line of code. It's pretty arbitrary to say "you can have one-line anonymous functions, but for two lines, the function must have a name". No other language does that, because it makes very little sense. This proposal will make Python behave more like every other programming language that supports anonymous functions.
- aceofspades19 18y agoI just don't understand where you would need an anonymous function with more then one line
- russell 18y agoThe usage case is the same as for a named function of more than one line.
- sah 18y agoYou wouldn't want to have to declare named functions for the bodies of built-in control flow constructs like if/else statements and for loops, right? All of the reasons why also apply to why you might not want to declare named functions in order to pass them to other functions. Read some Ruby code that uses blocks for an example of how passing anonymous functions can become a very clear and natural idiom. In Ruby, you can write your own simple control flow constructs in the form of functions that take a block -- in fact the idiomatic way to iterate through a collection in Ruby is with a function rather than a built-in control flow construct. In languages like Lisp and Smalltalk, where functions can naturally take more than one in-line block, you can write any control flow construct yourself.
- bobbyi 18y agoBut how would this new syntax work when you want to pass two or more anonymous functions to another function? Or when the anonymous function is not the last argument? Having anonymous functions only for the situation where you are passing exactly one of them to another function and it is the last argument is much more arbitrary than the current style of always temporarily binding functions to names.
- russell 18y agoThis style takes care of 90 or 99% of the cases. If you have to pass 2 or more functions, revert to using named functions. The 'there' proposal that I mentioned before allows for arbitrary placement of the anonymous function, IIRC. If not, usage patterns will adjust to the new idiom.
- sah 18y agoYou might say by the same argument that single-line lambda expressions take care of 90 or 99% of cases, and you should revert to using named functions for the rest. I don't think either is true -- if you have more flexibility in the syntax, you'll do more with it.
- russell 18y agoThe reason for all the discussion over the years is that a single line lambda doesn't cover 90% of the cases. If you think in terms of callbacks, I bet one liner cover less than 10% of the cases.
- anewaccountname 18y agoPython already has a solution for callbacks: @reg_callback(button.onclick) def callback(): do_stuff()
- jamongkad 18y agoWhilst this is more pythonic I personally prefer Javascripts use of lambdas as more than a means of expression. More like a way of life for the language :-) And that is IMHO what makes it functionally closer to Scheme. Just looking at the way one defines lambdas in Javascript and their use throughout the language is a thing of beauty.
- halo 18y agoThe reason no other language does that is because no other mainstream language uses newline and indentation to indicate blocks, which makes all general-purpose multi-line anonymous function syntaxes inherently ugly and non-Pythonic: where should the new block's indentation begin or end? I also regard it as a relatively minor problem as there's little cognitive overhead from simply naming a multi-line function, and I can see advantages in encouraging naming for complex functions. Having thought about it for a couple of hours, I don't think there is any good solution for adding multi-line anonymous functions to Python. The suggested syntax feels non-Pythonic, is inherently unexpressive and difficult to read, only useful in a few select use-cases while not solving the holy grail of 'pleasant anonymous functions', and the single problem it's solving isn't common enough or big enough to need dedicated syntax and could already largely be solved with a named function. The fact it enables 'misuse' via Ruby-style iteration, going against Pythonic ideals of 'one way to do things', doesn't work in its favour either. I don't think analogies with decorators are good either. The advantages of decorators is they transform something otherwise difficult-to-read and unexpressive into something easier-to-read and more expressive, whereas this makes something that's easy-to-read less expressive with the only advantage being terseness. Python, unlike other languages, focuses primarily on clarity over code length.
- jrockway 18y agoThe reason no other language does that is because no other mainstream language uses newline and indentation to indicate blocks, which makes all general-purpose multi-line anonymous function syntaxes inherently ugly Haskell is newline-and-indentation-based and allows multi-line anonymous functions. With that in mind, I often prefer to name functions, but not let them be globally callable, i.e. f = (g . h) where g = (2*) h = (1+) Does Python allow something like this? (Equivalents in other languages: (defun f (x) (flet ((g (x) (* 2 x)) (h (x) (1+ x))) (g (h x)))) Or: sub f { my $g = sub { 2 * $_[0] }; my $h = sub { 1 + $_[0] }; return $g->($h->($_[0])); } I think Haskell is prettiest, but this is not a concept unique to Haskell, and it's a technique I find useful in all three languges.)
- sah 18y ago
- gaius 18y agoI don't understand why naming a function is such a problem? You can even do it in the local scope and prefix it _ if you want to.
- SapphireSun 18y agoI also don't understand why people use lambda like: myfunction( lambda a: a.foo > a.bar ) I think the following looks neater: aname = lambda a: a.foo > a.bar myfunction( aname ) I think that putting unnecessary clutter inside of a function call is just asking for readability problems.
- sofal 18y agoIf you explored outside of your boundaries and learned different ways of programming (like functional programming), you might realize that there are perfectly valid styles of programming where lambdas are as common as if-statements and for-loops. I think that it is reasonable to assume that creating named local variables for each and every one of them would be silly.
- jrockway 18y agoTo be fair to the grandparent, Python goes out of its way to discourage use of functional programming techniques.