[Twisted-Python] "Old style" classes in Twisted and inheritance

Atilla theatilla at gmail.com
Tue Nov 13 07:07:21 MST 2007


I hope this is the correct place for this question.

I was trying to create a little customized task that sends a (ping)
message to the clients every X seconds. Because I needed some extra
bookkeeping, I implemented it as a class instance with a __call__
method, that is passed to the LoopingCall.

However, I also wanted to automatically start the task on init, so I
thought - it makes a lot of sense to simply Inherit it, call its
constructor and pass self as the parameter. It was then I realized
that the classes in twisted are old-style, when my super() call didn't
work.

Is there any practicular reason for that? Am I wrong to try inherit
the Looping Call ? Basically my code looks like :

class SillyPinger(object):
        def __init__(self, factory):
            self.factory = factory
            self.pingTargets = {}

            pingTask = task.LoopingCall(self)
            pingTask.start(5.0)

        def __call__(self):
            # do stuff
            pass


while I'd like to be doing:

class SillyPinger(task.LoopingCall):
        def __init__(self, factory):
            super(SillyPinger, self).__init__(self)

            self.factory = factory
            self.pingTargets = {}

            self.start(5.0) # get value from .ini here


Should I just switch to calling the LoopingCall constructor in the old
way? I'm not sure I'd like to do that, even if only for consistency
reasons.




More information about the Twisted-Python mailing list