2
我想动态创建一个类使用type()
并指定一个__init__
构造函数调用super().__init__(...)
;然而,当super()
被调用我收到以下错误:动态类继承使用超
TypeError: super(type, obj): obj must be an instance or subtype of type
这里是我的代码:
class Item():
def __init__(self, name, description, cost, **kwargs):
self.name = name
self.description = description
self.cost = cost
self.kwargs = kwargs
class ItemBase(Item):
def __init__(self, name, description, cost):
super().__init__(name, description, cost)
def __constructor__(self, n, d, c):
super().__init__(name=n, description=d, cost=c)
item = type('Item1', (ItemBase,), {'__init__':__constructor__})
item_instance = item('MyName', 'MyDescription', 'MyCost')
为什么super()
内__constructor__
方法不理解对象参数;我该如何解决它?