[Twisted-Python] lambdas and DeferredList

Henrik Thostrup Jensen thostrup at gmail.com
Fri Aug 3 02:38:54 MDT 2007


On 8/3/07, gary jefferson <garyjefferson123 at gmail.com> wrote:
>
> I don't quite understand the behavior of lambdas in the following code:

[snip]

>
> def dlistspawn():
>     dlist = []
>     values = [(1, 'one'), (2, 'two'), (3, 'three')]
>     for i, v in values:
>         deferred = someDeferred(i)
>         deferred.addCallback(onething, i, lambda: twothing(v))
>         dlist.append(deferred)
>     dl = defer.DeferredList(dlist)
>     dl.addCallback(done)
>     return dl
>
> if __name__ == "__main__":
>     dlistspawn()
>     reactor.run()
>
>
> Which produces:
> onething: 1, result 1
> twothing: three
> onething: 2, result 2
> twothing: three
> onething: 3, result 3
> twothing: three
> all done
>
>
> Why does the call to twothing() (via a lambda and callableThing)
> always bind to 'three'?  How do I change the code to get it to bind
> successively to 'one', 'two', and 'three'?


The variable v gets bound when the lambda is invoked, not when constructed.


-- 
   - Henrik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20070803/2090d2fa/attachment.html>


More information about the Twisted-Python mailing list