Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个变量,Python中继承?_Python_Oop_Inheritance - Fatal编程技术网

从一个变量,Python中继承?

从一个变量,Python中继承?,python,oop,inheritance,Python,Oop,Inheritance,我想告诉一个类每次使用它时要从中继承什么 这里有一个例子 class widget(var): def __init__(self,var): #I really have no idea bob=widget(wx.ComboBox) #I want the class to inheirit from wx.ComboBox steve=widget(wx.ListBox) #I want the class to inheirit from wx.ComboBox

我想告诉一个类每次使用它时要从中继承什么

这里有一个例子

class widget(var):
    def __init__(self,var):
        #I really have no idea

bob=widget(wx.ComboBox) #I want the class to inheirit from wx.ComboBox
steve=widget(wx.ListBox) #I want the class to inheirit from wx.ComboBox

请不要问“为什么要这样做?”

每次从任何一个基类创建子类时,您都需要创建一个不同的类。通过一个动态创建类的函数,您可以轻松做到这一点:

def widget(var):
   class Widget(var):
       pass
   return Widget()
每次从任何一个基类创建子类时,都需要创建一个不同的类。通过一个动态创建类的函数,您可以轻松做到这一点:

def widget(var):
   class Widget(var):
       pass
   return Widget()

希望,这将帮助您:

class A:
    v = 'a'

class B:
    v = 'b'

def C(x):
    class C(x): pass
    return C
x = A
print C(x).v

UPD注意:如果您还没有查看,请查看。

希望,这将帮助您:

class A:
    v = 'a'

class B:
    v = 'b'

def C(x):
    class C(x): pass
    return C
x = A
print C(x).v

UPD注意:如果还没有,请查看。

为什么要这样做?(这是不可避免的:)我想你要找的是“装饰模式”,而不是继承。或者是一个设置默认值的工厂。你为什么要这样做?(这是不可避免的:)我想你要找的是“装饰模式”,而不是继承。或者是一个设置默认值的工厂。