【发布时间】:2019-05-15 23:25:26
【问题描述】:
Python 2.7.10
嗨,
我想做的就是继承超类属性,这是一个标准的面向对象的事情。
从我可以在这里和其他地方在线找到的内容来看,这应该可以:
class SubClass(MyParentClass):
def __init__(self):
super(SubClass, self).__init__()
得到:
TypeError: must be type, not classobj
那怎么不是类型?我提出这个问题:
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
我无法将我的大脑包裹在那个人身上。对象实例不是其类类型的实例吗?这怎么可能?
任何帮助将不胜感激。
【问题讨论】:
-
这可能只是在 python 2.7 中的东西,因为它在 python 3.6 中对我来说很好。
-
如果
SubClass.__init__除了调用另一个__init__之外没有做任何其他事情,它可以完全省略。方法本身将被继承和调用。
标签: python inheritance attributes python-2.x super