[Twisted-Python] the sequence of executing defers' callbacks

Christopher Armstrong radix at twistedmatrix.com
Fri May 23 06:51:02 MDT 2008

On Thu, May 22, 2008 at 11:20 PM, Xian Chen <hoganxian at gmail.com> wrote:
> As you said, what I really want is a dispatcher which can dispatch different
> msgs to corresponding callbacks.
> Of course, I can write a dispatchMessage(msg) function by using lots of "if
> ... else ", but I want a more elegant way. I hope defers can help me.

Here's an idiom that's served me well in the past: instead of using
lots of 'if...else', use getattr(self, 'handle_' +
messageName)(message). Replace 'handle_' with something appropriate
for your message system.

Decorators are also getting common as a way to register handlers for
particular types of messages.

Christopher Armstrong
International Man of Twistery

More information about the Twisted-Python mailing list