[Python-Dev] Let's just *keep* lambda

Adam Olsen rhamph at gmail.com
Thu Feb 9 23:52:06 CET 2006


On 2/9/06, Valentino Volonghi aka Dialtone <dialtone at divmod.com> wrote:
> Let's consider this piece of code (actual code that works today and uses
> twisted for convenience):
>
> def do_stuff(result):
>     if result == 'Initial Value':
>         d2 = work_on_result_and_return_a_deferred(result)
>         d2.addCallback(println)
>         return d2
>     return 'No work on result'
>
> def println(something):
>     print something
>
> d1 = some_operation_that_results_in_a_deferred()
> d1.addCallback(do_stuff)
> d1.addCallback(lambda _: reactor.stop())
>
> reactor.run()

PEP 342 provides a much better alternative:

def do_stuff():
    result = (yield some_operation())
    something = (yield work_on_result(result))
    print something
    reactor.stop()  # Maybe unnecessary?

reactor.run(do_stuff())

Apparantly it's already been applied to Python 2.5:
http://www.python.org/dev/doc/devel/whatsnew/node4.html

Now that may not be the exact syntax that Twisted provides, but the
point is that the layout (and the top-to-bottom execution order) is
possible.

--
Adam Olsen, aka Rhamphoryncus


More information about the Python-Dev mailing list