[英]super() fails with error: TypeError "argument 1 must be type, not classobj" when parent does not inherit from object
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:仅供参考,这是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)
|
Your problem is that class B is not declared as a "new-style" class.您的问题是 B 类未声明为“新型”类。 Change it like so:
像这样改变它:
class B(object):
and it will work.它会起作用。
super()
and all subclass/superclass stuff only works with new-style classes. super()
和所有子类/超类的东西只适用于新式类。 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.我建议您养成始终在任何类定义上键入该
(object)
的习惯,以确保它是一个新型类。
Old-style classes (also known as "classic" classes) are always of type classobj
;旧式类(也称为“经典”类)始终是
classobj
类型; new-style classes are of type type
.新式类的类型为
type
。 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:试试这个,看看你自己:
class OldStyle:
pass
class NewStyle(object):
pass
print type(OldStyle) # prints: <type 'classobj'>
print type(NewStyle) # prints <type 'type'>
Note that in Python 3.x, all classes are new-style.请注意,在 Python 3.x 中,所有类都是新样式的。 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.
所以,在 Python 3.x 中你不会有这个问题。
Also, if you can't change class B, you can fix the error by using multiple inheritance.此外,如果您无法更改 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)
If the python version is 3.X, it's okay.如果python版本是3.X就可以了。
I think your python version is 2.X, the super would work when adding this code我认为您的python版本是2.X,添加此代码时超级会起作用
__metaclass__ = type
so the code is所以代码是
__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)
I was also faced by the posted issue when I used python 2.7.当我使用 python 2.7 时,我也遇到了发布的问题。 It is working very fine with python 3.4
它在 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.为了让它在 python 2.7 中工作,我在我的程序顶部添加了
__metaclass__ = type
属性并且它起作用了。
__metaclass__
: It eases the transition from old-style classes and new-style classes. __metaclass__
:它简化了旧式类和新式类的转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.