首页 > 解决方案 > 将创建动态类的函数转换为 Python 中的动态包装类

问题描述

我遇到了一个选择性继承类的解决方案。虽然这是一个很好的解决方案,但我想创建一个包装类而不是作为函数返回的类。具体来说,就我而言,因为它有点复杂,所以我有以下内容:

class A(object):
    def __init__(self, argM, argO1=0, argO2=''):
        # do something here

    def funA(self):
        # do something here


class B(A):
    def __init__(self, argO1=0, argO2=''):
        super(B, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funB(self):
        # do something here


class C(B):
    def __init__(self, argM, argO1=0, argO2=''):
        super(C, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funC(self):
        # do something here


class D(C):
    def __init__(self, argM, argO1=0, argO2=''):
        super(D, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funD(self):
        # do something here


class E(C):
    def __init__(self, argM, argO1=0, argO2=''):
        super(E, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funE(self):
        # do something here


class F(C):
    def __init__(self, argM, argO1=0, argO2=''):
        super(F, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funF(self):
        # do something here


class G(B):
    def __init__(self, argM, argO1=0, argO2=''):
        super(G, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funG(self):
        # do something herehere


class H(B):
    def __init__(self, argM, argO1=0, argO2=''):
        super(H, self).__init__(argM, argO1=0, argO2='')
        # do something here

    def funG(self):
        # do something here

为了创建一个包装类MyClass,我编写了上面链接中描述的以下函数:

def createMyClass(argM, objdetails, fun1=0, fun2=0, fun3=0, sup1=0, sup2=0):  # fun1, fun2, fun3 are mutually exculsive.
    det1 = objdetails['argO1']
    det2 = objdetails['argO2']

    if fun1:
        class Cf1(C):
            def __init__(self):
                super(Cf1, self).__init__(argM, argO1=det1, argO2=det2)
        if sup1:
            class Cs1(G):
                def __init__(self):
                    super(Cs1, self).__init__(argM, argO1=det1, argO2=det2)
            if sup2:
                class Cs2(H):
                    def __init__(self):
                        super(Cs2, self).__init__(argM, argO1=det1, argO2=det2)
                class MyClass(Cf1, Cs1, Cs2):
                    def __init__(self):
                        super(MyClass, self).__init__()
            else:
                class MyClass(Cf1, Cs1):
                    def __init__(self):
                        super(MyClass, self).__init__()
        else:
            class MyClass(Cf1):
                def __init__(self):
                    super(MyClass, self).__init__()

    if fun2:
        class Cf1(D):
            def __init__(self):
                super(Cf1, self).__init__(argM, argO1=det1, argO2=det2)
        if sup1:
            class Cs1(G):
                def __init__(self):
                    super(Cs1, self).__init__(argM, argO1=det1, argO2=det2)
            if sup2:
                class Cs2(H):
                    def __init__(self):
                        super(Cs2, self).__init__(argM, argO1=det1, argO2=det2)
                class MyClass(Cf1, Cs1, Cs2):
                    def __init__(self):
                        super(MyClass, self).__init__()
            else:
                class MyClass(Cf1, Cs1):
                    def __init__(self):
                        super(MyClass, self).__init__()
        else:
            class MyClass(Cf1):
                def __init__(self):
                    super(MyClass, self).__init__()

    if fun3:
        class Cf1(E):
            def __init__(self):
                super(Cf1, self).__init__(argM, argO1=det1, argO2=det2)
        if sup1:
            class Cs1(G):
                def __init__(self):
                    super(Cs1, self).__init__(argM, argO1=det1, argO2=det2)
            if sup2:
                class Cs2(H):
                    def __init__(self):
                        super(Cs2, self).__init__(argM, argO1=det1, argO2=det2)
                class MyClass(Cf1, Cs1, Cs2):
                    def __init__(self):
                        super(MyClass, self).__init__()
            else:
                class MyClass(Cf1, Cs1):
                    def __init__(self):
                        super(MyClass, self).__init__()
        else:
            class MyClass(Cf1):
                def __init__(self):
                    super(MyClass, self).__init__()

    return MyClass

这可以通过调用来创建一个包装类:

obj1 = {'argO1': 0, 'argO2': 'Batter'}
C1 = createMyClass("First", obj1, fun1=1, sup1=1)
obj2 = {'argO1': 0, 'argO2': 'Pitcher'}
C2 = createMyClass("Starting", obj2, fun2=1, sup1=1, sup2=1)

但我想createMyClass用一个类替换该函数,该类MyClass负责简单地执行选择性继承的功能而没有其他方法/函数。这样我就可以做到:

obj1 = {'argO1': 0, 'argO2': 'Batter'}
C1 = MyClass("First", obj1, fun1=1, sup1=1)
obj2 = {'argO1': 0, 'argO2': 'Pitcher'}
C2 = MyClass("Starting", obj2, fun2=1, sup1=1, sup2=1)

我怎样才能做到这一点?

标签: pythonpython-2.7classinheritancemultiple-inheritance

解决方案


推荐阅读