views:

66

answers:

1
d = Deferred()
d.callback(Deferred()) # Assertion error saying that a Deferred shouldn't be passed

Why is this? I looked through the code and commit messages / Trac and see no reason why this should be the case. The most obvious way to bypass this is to put the Deferred in a tuple, but why is this restriction here in the first place?

+3  A: 

There are two related reasons for this.

First, it helps catch what is likely a mistake early - near the place where the mistake is being made. A Deferred is called back with a result which is then passed to all of its callbacks. If you make the result itself a Deferred, then there's not much these callbacks can do when they're called. This leads me to the next reason.

Second, Deferreds support be chaining which handles the most common use cases one might have for passing in a Deferred. Given two Deferreds, a and b, chaining causes a to pause processing its own callback chain until b has a result, then a resumes its callback chain with the result of b. This is what happens when a callback on a Deferred returns a Deferred. It can also be done explicitly with Deferred.chainDeferred.

Jean-Paul Calderone