Let’s say you want to create a class that includes mixins having names that are known at execution time only, you would do some thing like this :
1 |
class_with_mixins = type("my_class_name", tuple(mixins_classes+[BaseClass]), {}) |
To get mixins_classes, juste use importlib to get mixins classes from names.
A function that would create an instance of such a dynamic class would be something like this :
1 2 3 4 5 6 7 |
from importlib import import_module def get_instance_with_mixins(plugin_class_name,mixins_name,base_class,*args,**kwargs): mixins_classes = [ import_module(m) for m in mixins_name ] cls = type(plugin_class_name, tuple(mixins_classes+[base_class]), {}) instance = cls(*args,**kwargs) return instance |