How to change baseclass

class, dynamic, python

Solution

You could have a factory for your classes:

def completefactory(baseclass):
    class AutoComplete(baseclass):
        pass
    return AutoComplete

And then use:

TextAutoComplete = completefactory(TextCtrl)
PriceAutoComplete = completefactory(PriceCtrl)

On the other hand depending on what you want to achieve and how your classes look, maybe AutoComplete is meant to be a mixin, so that you would define `TextAutoComplete` with:

class TextAutocomplete(TextCtrl, AutoComplete):
    pass

Problem

I have a class which is derived from a base class, and have many many lines of code e.g. ``` class AutoComplete(TextCtrl): ..... ``` What I want to do is change the baseclass so that it works like ``` class AutoComplete(PriceCtrl): ..... ``` I have use for both type of AutoCompletes and may be would like to add more base classes, so how can I do it dynamically? Composition would have been a solution, but I do not want to modify code a lot. any simple solutions?

Original source