在Python中选择继承自哪个父类

在Python中选择继承自哪个父类,python,class,inheritance,Python,Class,Inheritance,我正在努力尝试在创建类时选择要继承的父类,而不影响任何子类 类有一个金字塔结构,在顶部,我尝试选择从何处继承:threading.Thread或multiprocessing.Process(使用子类上的参数指定它) 我只在金字塔的第一层创建了一个返回基类的函数,代码如下: class ProcessClass(object): '''A thread with different inheritance. ''' def __new__(cls, *args, **kw

我正在努力尝试在创建类时选择要继承的父类,而不影响任何子类

类有一个金字塔结构,在顶部,我尝试选择从何处继承:
threading.Thread
multiprocessing.Process
(使用子类上的参数指定它)

我只在金字塔的第一层创建了一个返回基类的函数,代码如下:

class ProcessClass(object):
    '''A thread with different inheritance.
    '''
    def __new__(cls, *args, **kwargs):
        thr_type = kwargs.pop("thr_type","threading")
        print("HELLO", thr_type)
        if(thr_type == "threading"):
            new_cls = threading.Thread 
        if(thr_type == "multiprocessing"):
            new_cls = multiprocessing.Process
        instance = super(ProcessClass, cls).__new__(obtain_thread_class(new_cls))
        instance.__init__(*args, **kwargs)
        return instance

def obtain_thread_class(new_cls):
    class BaseClass(new_cls):
        """Class with its methods"""
        def __init__(self, daemon=False, *args, **kwargs):
            # THREADING
            super(BaseClass, self).__init__(*args, **kwargs)
            self.daemon=daemon
        def hello(self):
            print("Hello World")
    return BaseClass
但是现在,当我尝试在子类中继承
ProcessClass
时,这个类失去了所有的属性,并且无法正确地传递参数(我认为这是由于
\uuuuuu new\uuu

子示例类是:

class ExampleClass(ProcessClass):
    def __init__(self, arg1, arg2, arg3="1", *args, **kwargs):
        self.list_args = [arg1, arg2]
        print("arg3 is", arg3)
        super(ExampleClass, self)
    def hello2(self):
        print("Hello from child!")
有了这个,如果我执行:

a = ExampleClass(thr_type="threading")
print(type(a))
我获得:

HELLO threading
<class '__main__.obtain_thread_class.<locals>.BaseClass'>
我认为使用
\uuuuuu new\uuuuuuu
是破坏儿童创造的东西

我也尝试过使用decorator,看到了一些
\uuuu元类\uuuuu
,但在深入了解这些领域之前,我想知道是否有更简单的方法,因为我不是python方面的专家


此外,我不希望此更改影响我正在运行的任何程序(许多类是从
ProcessClass
继承的,但目前这只是
threading.Thread
)。我正在努力使整个程序中的这一变化
不被注意。

最后我选择了作文。未找到如何在运行时选择继承。但这仍然有效

class ProcessClass(object):
    '''A thread with different inheritance.
    '''
    def __init__(self, thr_type="multiprocessing", *args, **kwargs):
        if(thr_type == "threading"):
            new_cls = threading.Thread
        if(thr_type == "multiprocessing"):
            new_cls = multiprocessing.Process
        self.thread = new_cls(*args, **kwargs)
    def start(self):
        return self.thread.start()

    def run(self):
        return self.thread.run()

您是否考虑过为不同的进程类型使用组合而不是使用继承?也许:这种方法没有任何意义:只有一个类
ExampleClass
,这是不够的(如果您希望对象实际将这些派生类作为其类型,以便方法查找工作),对于由
ProcessClass
创建的每个对象,都有一个单独的
BaseClass
,这也是错误的。您是在寻求自动生成类或编写工厂函数的帮助,还是在寻求其他帮助?我正试图找出如何使用参数简单地确定
ProcessClass
的父类
BaseClass
以前是在
ProcessClass
中,但是由于我返回了一个带有
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
class ProcessClass(object):
    '''A thread with different inheritance.
    '''
    def __init__(self, thr_type="multiprocessing", *args, **kwargs):
        if(thr_type == "threading"):
            new_cls = threading.Thread
        if(thr_type == "multiprocessing"):
            new_cls = multiprocessing.Process
        self.thread = new_cls(*args, **kwargs)
    def start(self):
        return self.thread.start()

    def run(self):
        return self.thread.run()