3 ms·
I never got the argument about one-line lambdas... Every time I needed a lambda, it was either one line, or deserved a new function (even if nested). Are there
by viraptor 17y ago
I never got the argument about one-line lambdas... Every time I needed a lambda, it was either one line, or deserved a new function (even if nested). Are there some serious cases where it's not enough?
- jmillikin 17y agoAnd it's not even correct. Python's lambda can contain an arbitrary number of lines, as long as they're a single expression. Considering that lambda is "borrowed" from functional languages, which are inherently single-expression, I've never seen what anybody's problem is with them.
- brehaut 17y agoThe problem is that in functional languages you have a lot of tools build on higher order functions, so you can easily leverage functions as expressions. On the other hand, its standard practice to use statement forms (if, while, for, try etc) to describe a lot of the processing. As statements are not allowed in lambdas this means you either are stuck writing a local named function and writing the code in idiomatic python or writing some helper functions to allow you to write code in a functional but non-idiomatic python style. It comes down to a matter of style. My personal theory is that once a python programmer discovers the magic of itertools they are about 3 months from looking for a new language that supports the functional style more naturally.
- stcredzero 17y agoMulti-line, multi-statement closures are tremendously useful in Smalltalk. They're the "secret sauce" that makes Don't Repeat Yourself easy to pull off. You can use them to implement your own control structures, your own notion of transactions, etc. And all (or most) of the above esoteric hackery looks like ordinary Smalltalk. People are down on I statement Lambdas because they don't do all of the above so well.
- llimllib 17y agoOK, I'm a huge python homer, but I'll take a hack at that. Sometimes, for reasons of style and clarity, I want to define a function inline, but python doesn't allow me to. It forces me to do: def x(bar, fram): z = foo(bar, fram) baz(z) button.onClick(x) When in some cases, I would like to express it as: button.onClick( def (bar, fram): z = foo(bar, fram) baz(z) ) So I get that it's never necessary, but I find it sometimes limiting as an author who cares strongly about how his code is organized.
- jmillikin 17y agoThe example given (I know it's synthetic, but still) can be written easily as a lambda: button.onClick(lambda bar, fram: baz(foo(bar, fram))) The problem with embedding procedure definitions within parameter lists is indentation. Languages such as JavaScript or Ruby use explicit syntax for indentation, which allows a degree of flexibility in how procedure literals are defined. Python uses only whitespace, so trying to extend the grammar to support arbitrary procedure literals would be very difficult (according to Guido).
- mbreese 17y agoI kinda like the way syntax the parent used. It seemed to make sense and work with the whitespace.
- llimllib 17y agofor the same reasons of style and clarity, I prefer my imaginary syntax to the lambda you've defined. And if you want to make it impossible, just imagine it's "print baz(z)". Anyway, I'm familiar with why Guido doesn't want them, and I totally respect it, and I write 99% of my personal code in python, so I clearly don't think it's a huge deal. It's just something I wish python did have, is all.
- viraptor 17y agoI see the problem when using third-party libraries, but if I was designing my own... Objects are pretty cheap in python: class Somebutton(Button): def onClick(self): ..... def onSomethingElse(self): ..... only one line of overhead (class line). And I'm pretty sure you can hack __dict__ in init, or use metaclasses so that the events are chained automatically, even without calling `Button.onClick(self)`. You can construct / add the object to it's container the same way (Somebutton(), instead of Button())
- malkia 17y agoI only ever use short-lambdas in map functions (common lisp), or long-lambdas (for the :callbacks) again in lispworks (a common-lisp implementation) CAPI (GUI from the company that made LispWorks). Once you have good syntax established for using lamdba-s in any form - they would be used. First users would be as I've put above - callbacks of sort (GUI, IO, etc.)