【发布时间】:2016-03-12 14:32:52
【问题描述】:
我正在尝试使用type() 动态创建一个类并分配一个调用super().__init__(...) 的__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')
为什么__constructor__方法里面的super()不理解对象参数;以及如何解决?
【问题讨论】:
标签: python-3.x object inheritance superclass