[Twisted-Python] Updated defer.html

Andrew Bennetts andrew-twisted at puzzling.org
Tue Mar 11 07:29:34 EST 2003


On Tue, Mar 11, 2003 at 12:26:38AM -0500, Christopher Armstrong wrote:
> On Mon, Mar 10, 2003 at 09:36:30PM -0500, Bob Ippolito wrote:
> > class Passthrough:
> > 	def __init__(self, fn):
> > 		self.fn = fn
> > 	def __call__(self, result, *args, **kwargs):
> > 		self.fn(*args, **kwargs)
> > 		return result
                ^^^^^^^^^^^^^  Note this line
> > 
> > Is something like that worthy of sitting in defer.py ?
> 
> Well, for one, it could be implemented like this:
> 
>   def passThrough(fn):
>       return lambda *args, **kwargs: fn(*args, **kwargs)

This will return None, not the result of the last callback (or errback).
Still, I think the above would be better as:

    from __future__ import nested_scopes

    def passThrough(fn):
        def foo(result, *args, **kwargs):
            fn(*args, **kwargs)
            return result
        return foo

Although wrapping the call to fn in a try/finally may be desirable.

> for two, I don't think it's worth it when people can just in-line a
> similar lambda into their code. I use stuff like this all the time:
> 
>   d.addCallback(lambda r: foo(bar, baz))

Well, I used to use:

    d.addCallback(lambda r: foo(bar, baz) or r)

But I soon found that it was way too hard to read when the expressions got
complex -- variations like "[foo(bar, baz)] and r" just weren't worth the
pain.

-Andrew.





More information about the Twisted-Python mailing list