[Python-ideas] Query about proposed Multi-line Anonymous Function syntaxes

Andrew Barnert abarnert at yahoo.com
Sat Oct 11 01:26:20 CEST 2014


On Oct 10, 2014, at 11:42, Guido van Rossum <guido at python.org> wrote:

> On Fri, Oct 10, 2014 at 8:06 AM, Adam Jorgensen <adam.jorgensen.za at gmail.com> wrote:
>> 
>> So, as I understand it, the main reason we don't have multi-line anonymous functions is because there has been no consensus about 
>> the syntax.
> 
> Well, it's worse -- it's hard to propose a syntax that actually works without huge changes to the way Python's parser treats indentation: inside parentheses, indentation is currently *not* significant.

As a somewhat-related data point, compare the indentation rules in the standard Apple style for Objective C (as strongly encouraged by Xcode) before and after the addition of blocks (which are close enough to multi-line lambdas for this purpose). I don't know a single person who sat down at Xcode 3, or emacs ObjC-mode, or any of a half-dozen other assistive IDEs that all applied the same rules, and didn't pick up those rules within a few minutes of coding. But since blocks, there are full-time ObjC developers who are now on their third generation of Xcode versions and still fighting with it trying to start their code at column 70, and the rules in all those different IDEs are no longer consistent.

Throwing suites into the middle of expressions inherently makes indentation more complicated. That's not to say that someone from the Python community couldn't solve this problem that Apple, Eclipse, JetBrains, etc. have failed at, just that the presumption going in shouldn't be that it's a trivial issue that's being used to unfairly dismiss a good idea.

> As for myself, I have encountered a number of languages that do support multi-line anonymous functions (Java, JavaScript, CoffeeScript) and I have to say that for some reason they do not lead to very readable code. Especially in the latter two, the prevalent use for anonymous functions is for asynchronous I/O callbacks. I find Python 3.4's "yield from" as used by the asyncio library leads to much more readable code, without he need for callback functions at all.

A lot of code in these languages uses different techniques to flatten out the "callback hell". The most prevalent is probably deferred/promise objects. Although these are borrowed from Twisted from the Python world, the way they interact with JS-like syntax arguably makes them more useful there: Every callback is defined as the argument to a method on a promise, which means they start in a consistent place, toward the left edge of the screen, etc., and they rarely nest, which avoids most of the problems with inline function definitions while still allowing the benefits (not having to define the functions out of order, or give them meaningless names).

Of course you could easily come up with a syntax based on one of Nick's two PEPs that's even nicer. And the fact that Python has an even better solution already (asyncio-style) makes it less interesting. Also, JS definitely encourages novices to write unreadable horrible code, even if it allows experienced developers to write it more cleanly, and I don't think that's a balance Python wants.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141010/dc3b725b/attachment.html>


More information about the Python-ideas mailing list