multiple extensions to a class

David Fraser davidf at sjsoft.com
Fri Jul 9 17:12:41 EDT 2004


Jeff Shannon wrote:
> Humpty Dumpty wrote:
> 
>> One possibility would be to define the extensions B,C and D as 
>> functions and
>> add those functions to A with new.instancemethod. Any other ideas on 
>> how to
>> do this? E.g. is it possible to *add* base classes to a class? I could 
>> add
>> the extensions by adding them as base classes to A.
>>  
>>
> 
> You might consider making class A into a container of some sort -- i.e., 
> give it someplace to store a list of "decorator" objects, and then make 
> B, C, and D into such decorators (rather than subclasses of A).  You'll 
> have to arrange some way of dispatching messages to A that are intended 
> for the decorators, and possibly some way for the decorators to talk to 
> each other.  (It's hard to say whether you'd be better off making B and 
> C separate decorators, or whether to subclass C from B -- depends on 
> your specific application.)
> 
The advantage of Python here is that you don't need to subclass one from 
the other - just make a clear definition of the methods you expect, and 
then have a system where you call methods on a list of listening classes...

David



More information about the Python-list mailing list