multiple extensions to a class
oliver.schoenborn at utoronto.ca
Mon Jul 12 05:18:16 CEST 2004
From: "John Lenton" <jlenton at gmail.com>
> > I.e., if I have a class A, and I want to add a block of functionality,
> > can derive it into a B that adds that fucntionality. If I want to add
> > functionality, I can derive B into a C. But if I want to add a third bit
> > functionality D directly to A, such that D knows nothing about B or C, I
> > won't be able to instantiate an object that has all three extensions,
> > an "ABCD". It will either be an "ABC" or an "AD".
> > One possibility would be to define the extensions B,C and D as functions
> > add those functions to A with new.instancemethod. Any other ideas on how
> > do this? E.g. is it possible to *add* base classes to a class? I could
> > the extensions by adding them as base classes to A.
> maybe I'm missing something, but wouldn't a class E(ABC, AD) work?
No because B,C and D are discovered at run-time only, ie class E can't be
known until runtime. You'd have to create a string with the code and use
exec or eval or s/t similar.
More information about the Python-list