3 回答

TA贡献1810条经验 获得超4个赞
您的问题是类B没有声明为“新样式”类。像这样更改它:
class B(object):
它会工作。
super()并且所有子类/超类的内容仅适用于新型类。我建议您养成始终(object)在任何类定义上键入它的习惯,以确保它是一种新型的类。
旧式类(也称为“经典”类)始终为type classobj;新样式类的类型为type。这就是为什么您看到错误消息的原因:
TypeError: super() argument 1 must be type, not classobj
试试看自己:
class OldStyle:
pass
class NewStyle(object):
pass
print type(OldStyle) # prints: <type 'classobj'>
print type(NewStyle) # prints <type 'type'>
请注意,在Python 3.x中,所有类都是新样式。您仍然可以使用旧样式类中的语法,但是会获得新样式类。因此,在Python 3.x中,您将不会遇到此问题。

TA贡献1802条经验 获得超5个赞
另外,如果您不能更改类B,则可以使用多重继承来修复错误。
class B:
def meth(self, arg):
print arg
class C(B, object):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)

TA贡献1828条经验 获得超3个赞
如果python版本是3.X,就可以了。
我认为您的python版本是2.X,添加此代码后,超级版本将起作用
__metaclass__ = type
所以代码是
__metaclass__ = type
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
添加回答
举报