[Python-ideas] The async API of the future: Twisted and Deferreds

Laurens Van Houtven _ at lvh.cc
Sun Oct 14 18:18:52 CEST 2012


On Sun, Oct 14, 2012 at 5:53 PM, Guido van Rossum <guido at python.org> wrote:


> A readable version of this could should not have to use lambdas.
>

In a lot of Twisted code, it happens with methods as callback methods,
something like:

d = self._doRPC(....)
d.addCallbacks(self._formatResponse, self._formatException)
d.addCallback(self._finish)

That doesn't talk about gatherResults, but hopefully it makes the idea
clear. A lot of the legibility is dependant on making those method names
sensible, though. Our in-house style guide asks for limiting functions to
about ten lines, preferably half that. Works for us.

Another pattern that's frowned upon since it's a bit of an abuse of
decorator syntax, but I still like because it tends to make things easier
to read for inline callback definitions where you do need more than a
lambda:

d = somethingThatHappensLater()

@d.addCallback
def whenItsDone(result):
    doSomethingWith(result)


--Guido van Rossum (python.org/~guido)
>

cheers
lvh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121014/ede49a32/attachment.html>


More information about the Python-ideas mailing list