On 2/9/06, Valentino Volonghi aka Dialtone <[EMAIL PROTECTED]> 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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to