how to dynamically instantiate an object inheriting from several classes?
Arnaud Delobelle
arnodel at googlemail.com
Fri Nov 21 17:30:16 EST 2008
More information about the Python-list mailing list
Fri Nov 21 17:30:16 EST 2008
- Previous message (by thread): how to dynamically instantiate an object inheriting from several classes?
- Next message (by thread): how to dynamically instantiate an object inheriting from several classes?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Joe Strout <joe at strout.net> writes: > 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? Of course it's possible: use type(name, bases, dict). >>> class A(object): pass ... >>> class B(object): pass ... >>> C = type('C', (A, B), {}) >>> issubclass(C, A) True >>> issubclass(C, B) True Call-by-object'ly yours -- Arnaud
- Previous message (by thread): how to dynamically instantiate an object inheriting from several classes?
- Next message (by thread): how to dynamically instantiate an object inheriting from several classes?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list