[Twisted-Python] How to hook onto or append functionality to a class (from another class)?

Glyph Lefkowitz glyph at twistedmatrix.com
Thu Sep 9 20:36:37 EDT 2010


On Sep 9, 2010, at 4:27 PM, Einar S. Idsø wrote:

> Hi Glyph, and thank you for your thorough and very helpful response. :)
> 
> On Wed, Sep 8, 2010 at 9:47 PM, Glyph Lefkowitz <glyph at twistedmatrix.com> wrote:
>> It sounds like this is a pretty trivial application of the Publish/Subscribe
>> pattern (or perhaps the Observer) pattern.
> (...)
>> Add one general-purpose
>> "list of observers that want to be notified (when X happens)" argument.
>> Then when your getProcessOutput() finishes, just do 'for something in
>> self.observers: something.youJustGotServerStatus(newServerStatus)'.
> Yes! Perfect! That sounds like it's exactly what I need, thank you :)

Great, glad to help :).

>> It
>> usually makes sense to define an interface with a couple of interesting
>> methods when you do this, so that you can have more methods.
> Interfaces scare me, and I've been doing my best to avoid them in my
> application. The primary reason being that I am the only developer, so
> as long as I know what I'm doing I'll be alright. Although I'm not so
> sure that I actually do...

There are some cool implementation things you can do with interfaces, but they're a little obscure and you shouldn't worry about them.  It's most helpful to think of interfaces as just expanded / shared docstrings for things that are shared among many classes.

>> Please, please do not use t.p.hook.  I hope that we delete it one day.
> Thanks for the advice. I'll stay away.

Even greater.

>>> I hope this was somewhat comprehensible, and that there is an elegant
>>> solution out there. :)
>> Well, if my answer answers your question, then yes :).
> It certainly does, and it is much appreciated. I'll just pop out on
> Google for a few minutes to find some examples, and will get right
> back to coding my app. Thanks again for taking the time to help a
> novice out :)

It's always nice to have the help be appreciated.  Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100909/d391f5bc/attachment.htm 


More information about the Twisted-Python mailing list