[Python-3000] A plea for anonymous functions

Adam Olsen rhamph at gmail.com
Thu Nov 16 20:39:59 CET 2006


On 11/16/06, Antoine <solipsis at pitrou.net> wrote:
>
> 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).

This is a bit biased by my dislike of event-driven programming, but:

def x():
    response = yield server.do_command('get_synset_links')
    format_expanded(li.content, response.related)
event_loop.add(x())

It may be possible to clean it up further with decorators, anonymous
functions, or "suites", but it's interesting to note that the bulk of
the code contains no functions (anonymous or named) at all—one is only
used to wrap it all in a new "thread".

Another benefit is that any error handling can use traditional python
methods: a try/except wrapped around the operation that may fail.
TOOWTDI.

And yes, it does still require an event loop, but so does your
original javascript example; what do you think calls your callback?

-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-3000 mailing list