[Twisted-Python] Another nit on "Generalization of Deferred Execution in Python"

Andrew Bennetts andrew-twisted at puzzling.org
Fri Jan 17 17:43:42 EST 2003


On Fri, Jan 17, 2003 at 02:32:36PM -0600, Patrick K. O'Brien wrote:
> On Friday 17 January 2003 09:34 am, Tommi Virtanen wrote:
> >         http://cumulo-nimbus.mit.edu/deferex.xhtml
> >
> > 	You call this "pretty"?
> >
> > def prettyRequest(server, requestName):
> >     return server.makeRequest(requestName
> >                               ).addCallback(
> >         lambda result: ', '.join(result.asList())
> >         ).addErrback(
> >         lambda failure: failure.printTraceback())
> >
> > 	How about
> >
> > def prettyRequest(server, requestName):
> >     d=server.makeRequest(requestName)
> >     d.addCallback(lambda result: ', '.join(result.asList()))
> >     d.addErrback(lambda failure: failure.printTraceback())
> >     return d
> >
> > 	I'm pretty sure everyone will consider that more readable.
> 
> You've raised an issue I've wondered about as I've looked at some of the 
> Twisted source. There seems to be a fondness for returning large 
> expressions, rather than building up a return value in a more piecemeal 
> (and more easily read and understood) fashion. Is this simply a matter of 
> style, or is there something substantial going on here related to the 
> nature of Twisted? My own code tends to look more like your second example.

I find 
    d.addCallback(processResult).addErrback(reportError)
to be a nice and readable... but as soon as lambdas are involved, it tends
to get too messy, so I'll tend to use the piecemeal approach then.

So my answer is "it depends" :)

-Andrew.





More information about the Twisted-Python mailing list