python - 将创建动态类的函数转换为 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)
我怎样才能做到这一点?
解决方案
推荐阅读
- r - 如何从 R 中的池数据中删除特定月份?
- xml - XSL 错误:格式编号图片:被动字符不得出现在子图片中的活动字符之间
- mysql - 如何更改 MySQL Server 8.0 的 root 密码
- python - 如何从python中的阶乘数创建整数列表
- php - 试图在 demo.blade.php 中获取非对象的属性“id”
- python - 如何在起始词之间拉词直到 '\n' 新行。在python中提取单词时有什么方法可以放置'OR'条件?
- php - 在 Prestashop 1.7.6 中使用 Product 对象更新产品价格
- javascript - Material UI Autocomplete 组件未显示来自反应状态的值
- sql - 如何按顺序生成重复的数字
- node.js - MongoDB 节点 JS:MongoServerSelectionError