[Twisted-Python] addCallbacks issue

Stephen Thorne stephen at thorne.id.au
Wed Apr 14 22:22:09 EDT 2004


On Thu, Apr 15, 2004 at 11:11:28AM +1000, Andrew Bennetts wrote:
> On Tue, Apr 13, 2004 at 09:57:10PM +1000, Stephen Thorne wrote:
> > 
> > Maybe by making a parallel to imperitive style we can make it clearer.
> > 
> > d = op()
> > d.addCallbacks(callback, errback)
> > 
> > is equivilient to
> > 
> > try:
> >     r = op()
> > except e:
> >     return errback(e)
> > else:
> >     return callback(r)
> 
> The exact same thought occurred to me recently while I was lying in bed (and
> resolutely trying to go to sleep and not get up and send/read more email ;).  My
> only reservation is that some people might not be familiar with the
> try/except/else construct, but I suppose an appropriate link to the Python
> docs (probably a section of the Python tutorial?) can solve that.

Well, really, the try/catch/else construct was used here because I was
undecided about the clearest way of representing the concept.

We could write it like this

try:
    r = op()
except e:
    r = errback(e)
else:
    r = callback(r)
# continue to use 'r'.

And in fact, that was how I wrote it until I revised it to use 'return'
to explicitly say "this is the resulting value". We could rewrite it
like this:

try:
    r = op()
except e:
    return errback(e)

return callback(r)

Which doesn't use any constructs that an inexperienced python programmer
won't immediately recognise.

Regards,
Stephen Thorne.




More information about the Twisted-Python mailing list