[Python-3000] A plea for anonymous functions

Antoine solipsis at pitrou.net
Thu Nov 16 14:49:10 CET 2006


Hi,

> Well, if you find that easy to read and clear, I suspect you're more of
> a JavaScript programmer than a Python programmer.  Here's how a more
> general form of the above would look in Python:
>
>      response = (yield server.get_synset_links())
>      format_expanded(li.content, response.related)

This assumes there is an underlying event loop of some sort that accepts
and is able to schedule generators.
Much more annoyingly, all your "yield"'s must be in the same scope, they
can't be spread in various subfunctions called from the generator. It is a
massive limitation to writing clean code, unless your generators are very
short.

>      def handle_response(record):
>          format_expanded(li.content, response.related)
>      server.get_synset_link(handle_response)

As already mentioned, the problem with this idiom is that code that is
executed *after* appears *before* the "get_synset_link".
It makes the code less straightforward to write and especially to read
(and having readable code is important).

>      with async server.get_synset_link() as response:
>          format_expanded(li.content, response.related)

How is this one supposed to work ? What is "with async blah()" ?

> No, I said that under your proposal, you're trading two lines of code
> that looks like Python for two (or three) lines of code that looks like
> something else.

The problem is not merely related to the number of lines of code. It is a
readibility problem. Having the callback enclosed in a the function call
which registers it makes the code more readable than defining the callback
beforehand.
Saying this does *not* mean one is a Javascript programmer (I hate
Javascript, but this particular point of Javascript is superior to what
Python has to offer).

> So show us some sugar, then.  After all, Python's design is driven by by
> an urge to simplify real usage patterns that's been observed in the
> wild,

Real usage patterns of event-driven code can be seen everywhere from GUI
applications to network applications (written with Twisted).
In many applications it is arguably much more common than the
"try..finally" constructs that "with" attempts to simplify.

I'm not sure what more is needed here? Addresses of SVN repositories?

> Also see the "Patterns are signs of weakness in programming languages"
> discussions:

I would agree with this argument if I could understand how it relates to
the current discussion. Are you saying anonymous functions are a "design
pattern" ? (then what is *not* a design pattern ?).

> and note that Python's usually aiming for "(nearly) invisible", not
> "formal", in Norvig's taxonomy.

What is more "invisible", including the callback straight at the point
where it is registered, or defining it beforehand?


The only thing I can agree with here is that anonymous functions or code
blocks are very hard to design inside the current Python syntax. But
saying there is no point in anonymous functions is a plain lie.

Regards

Antoine.




More information about the Python-3000 mailing list