[Python-ideas] The Return of Implicit Lambda (Re: Allowing breaks in generator expressions by overloading the while keyword)

Andrew Barnert abarnert at yahoo.com
Mon Feb 24 09:21:45 CET 2014


From: Nick Coghlan <ncoghlan at gmail.com>
Sent: Friday, February 21, 2014 3:28 PM


>Yep, the implicit assumption was that the shorthand syntax could get away with handling just zero-argument functions (for simple callbacks and lazy evaluation) and one-positional-argument functions (for single object callbacks, sorting keys and filtering predicates).

>More complex cases don't need to be handled, as users have the option to revert to using an explicit lambda or a named function.
>This helps the proposal achieve one of useful heuristics to apply when judging a new piece of syntactic sugar: in the cases where it applies, it should be clearly superior to the existing alternatives. Once you get to 2+ arguments, it's hard to be clear without giving them names.


After more thought on this, I realized that this really does solve all of the issues with the idea. Sure, it looks ugly when trying to define a function as a value in a dict (which is a common thing to do)—so don't use it there. In some contexts the ? looks wrong, so don't use it there either. And so on. As long as there are plenty of use cases where it _would_ make code more readable—like, say, creating the callbacks in a long chain of Tkinter widget constructors—it's a win.

And, although it doesn't _actually_ solve the problem of removing the need for an extra abstraction, in many cases it _practically_ does. If you're reading those 20 widget constructors, yes, you do need to read and process the fact that a function is being created out of your expression on the first one—but on the next 19, your eye will just glide over it and you can read the expressions directly. Which solves the part of the problem that's worth solving.

And I'm not sure it's possible to _actually_ solve the problem anyway, without Python becoming a very different language. So I ended up writing the threatened blog post anyway: http://stupidpythonideas.blogspot.com/2014/02/fixing-lambda.html

So, I think it is worth reviving and extending PEP 312. What would help most in getting that idea considered? Gathering more concrete examples? Buiilding an implementation? Both?


More information about the Python-ideas mailing list