Python Forum

Full Version: Python dynamic inheritance ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ToggleWidgetClass = object

class SpecificToggleWdidgetClass(ToggleWidgetClass):
    def __init__(self, **kwargs):
        pass


def SpecificToggleWidget(var_ToggleWidgetClass = gtk.ToggleButton, **kwargs):
    global ToggleWidgetClass
    ToggleWidgetClass = var_ToggleWidgetClass

    return SpecificToggleWdidgetClass(**kwargs)
my problem is SpecificToggleWidgetClass not inheritancing again so it not changing when ToggleWidgetClass changed. is it impossible ?
You want a class factory?
class SomeBaseClass(object):
    def say_hello(self):
        print("Hi.")


class SomeOtherBaseClass(object):
    def say_hello(self):
        print("Yo.")


def class_factory(BaseClass):
    class SpecificClass(BaseClass):
        def __init__(self, *args, **kwargs):
            super(SpecificClass, self).__init__(*args, **kwargs)
    return SpecificClass
        

one = class_factory(SomeBaseClass)()
two = class_factory(SomeOtherBaseClass)()

for thing in (one,two):
    thing.say_hello()