Python 2.7.10
嗨
我只想继承super-class属性,这是一个标准的面向对象的东西。在
从我在这里和其他地方的网上资料来看,这应该是有效的:
class SubClass(MyParentClass):
def __init__(self):
super(SubClass, self).__init__()
得到:
^{pr2}$那怎么不是一种类型?我强调这个问题:
class SubClass(MyParentClass):
def __init__(self):
super(type(self.__class__), self).__init__()
得到:
TypeError: super(type, obj): obj must be an instance or subtype of type
我真想不起那件事。对象实例不是其类类型的实例吗?怎么可能呢?在
任何帮助将不胜感激。在
在Python2中,
super
只有在类层次结构从object
继承时才有效。在如果超类声明为
您将看到错误,因为创建的类是一个旧的0style类,它不支持
super
超类声明必须是
^{pr2}$例如:
在Python3中,旧样式的类已被删除,因此不再需要从对象显式继承。在
相关问题 更多 >
编程相关推荐