super() 失败并出现错误: TypeError “参数 1 必须是类型,而不是 classobj”当父对象不继承自对象时
我收到一些我无法弄清楚的错误。我的示例代码有什么问题吗?
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
我从“超级”内置方法的帮助下获得了示例测试代码。
这是错误:
Traceback (most recent call last):
File "./test.py", line 10, in ?
print C().meth(1)
File "./test.py", line 8, in meth
super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
仅供参考,这是来自 python 本身的帮助(超级):
Help on class super in module __builtin__:
class super(object)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super(C, self).meth(arg)
|
I get some error that I can't figure out. Any clue what is wrong with my sample code?
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
I got the sample test code from help of 'super' built-in method.
Here is the error:
Traceback (most recent call last):
File "./test.py", line 10, in ?
print C().meth(1)
File "./test.py", line 8, in meth
super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
FYI, here is the help(super) from python itself:
Help on class super in module __builtin__:
class super(object)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super(C, self).meth(arg)
|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题是 B 类没有声明为“新式”类。像这样改变它:
它会起作用。
super()
和所有子类/超类内容仅适用于新式类。我建议您养成在任何类定义上始终键入(object)
的习惯,以确保它是新式类。旧式类(也称为“经典”类)始终是
classobj
类型;新式类的类型为type
。这就是为什么您收到错误消息的原因:TypeError: super() argument 1 must be type, not classobj
尝试亲自查看:
请注意,在 Python 3.x 中,所有类都是新的-风格。您仍然可以使用旧式类的语法,但您会得到一个新式类。所以,在 Python 3.x 中你不会遇到这个问题。
Your problem is that class B is not declared as a "new-style" class. Change it like so:
and it will work.
super()
and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that(object)
on any class definition to make sure it is a new-style class.Old-style classes (also known as "classic" classes) are always of type
classobj
; new-style classes are of typetype
. This is why you got the error message you saw:TypeError: super() argument 1 must be type, not classobj
Try this to see for yourself:
Note that in Python 3.x, all classes are new-style. You can still use the syntax from the old-style classes but you get a new-style class. So, in Python 3.x you won't have this problem.
另外,如果无法更改类 B,则可以通过使用多重继承来修复错误。
Also, if you can't change class B, you can fix the error by using multiple inheritance.
如果python版本是3.X就可以了。
我认为你的python版本是2.X,添加此代码时超级会工作,
所以代码是
If the python version is 3.X, it's okay.
I think your python version is 2.X, the super would work when adding this code
so the code is
当我使用 python 2.7 时,我也遇到了发布的问题。它在 python 3.4 中工作得非常好
为了使其在 python 2.7 中工作,我在程序顶部添加了 __metaclass__ = type 属性,并且它工作了。
__metaclass__
:它简化了旧式类和新式类的转换。I was also faced by the posted issue when I used python 2.7. It is working very fine with python 3.4
To make it work in python 2.7 I have added the
__metaclass__ = type
attribute at the top of my program and it worked.__metaclass__
: It eases the transition from old-style classes and new-style classes.