how to dynamically instantiate an object inheriting from several classes?

Joe Strout joe at strout.net
Fri Nov 21 17:11:20 EST 2008


I have a function that takes a reference to a class, and then  
instantiates that class (and then does several other things with the  
new instance).  This is easy enough:

    item = cls(self, **itemArgs)

where "cls" is the class reference, and itemArgs is obviously a set of  
keyword arguments for its __init__ method.

But now I want to generalize this to handle a set of mix-in classes.   
Normally you use mixins by creating a class that derives from two or  
more other classes, and then instantiate that custom class.  But in my  
situation, I don't know ahead of time which mixins might be used and  
in what combination.  So I'd like to take a list of class references,  
and instantiate an object that derives from all of them, dynamically.

Is this possible?  If so, how?

Thanks,
- Joe




More information about the Python-list mailing list