News for hackers. Stuff that matters.
Re:Dear Python by viraptor on Tuesday February 02, 2010 @04:27PM (#1096178)
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?
Re:Dear Python by llimllib on Tuesday February 02, 2010 @05:02PM (#1096297)
OK, 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.
Re:Dear Python by jmillikin on Tuesday February 02, 2010 @05:09PM (#1096317)
The 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).
Re:Dear Python by llimllib on Tuesday February 02, 2010 @10:47PM (#1097006)
for 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.

Re:Dear Python by mbreese on Tuesday February 02, 2010 @05:23PM (#1096359)
I kinda like the way syntax the parent used. It seemed to make sense and work with the whitespace.
Re:Dear Python by j_baker on Tuesday February 02, 2010 @08:50PM (#1096830)
"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."

Nothing other than an if statement, a goto, and some basic arithmetic is needed in a programming language. It's all syntactic sugar. I agree that such a construct would be useful. However, it's more about if it's worth complicating the language over. I don't necessarily know how complex that would make things, but I can definitely see that a case can be made that it would be more trouble than it's worth considering the way indentation is handled.

Re:Dear Python by viraptor on Tuesday February 02, 2010 @05:12PM (#1096324)
I 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())
Re:Dear Python by 0x44 on Tuesday February 02, 2010 @09:45PM (#1096926)
Should you have to hack __dict__ in init to get functionality that would be more easily/clearly expressed using an anonymous function?
Re:Dear Python by moe on Wednesday February 03, 2010 @08:21AM (#1097663)
Not trying to defend python's decision here but I have honestly yet to see an example where anonymous function really led to better code.

In my experience (mostly JS and AS) they rather usually lead to little pieces of logic scattered all over the place and insufficient decoupling.

Re:Dear Python by jmillikin on Tuesday February 02, 2010 @04:30PM (#1096192)
And 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.
Re:Dear Python by brehaut on Tuesday February 02, 2010 @04:56PM (#1096279)
The 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.

Re:Dear Python by stcredzero on Tuesday February 02, 2010 @05:31PM (#1096385)
Multi-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.

Re:Dear Python by malkia on Tuesday February 02, 2010 @05:23PM (#1096360)
I 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.)