![](/img/trans.png)
[英]super() fails with error: TypeError "argument 1 must be type, not classobj" when parent does not inherit from object
[英]super() raises “TypeError: must be type, not classobj” for new-style class
以下使用super()
会引发TypeError:为什么?
>>> from HTMLParser import HTMLParser
>>> class TextParser(HTMLParser):
... def __init__(self):
... super(TextParser, self).__init__()
... self.all_data = []
...
>>> TextParser()
(...)
TypeError: must be type, not classobj
StackOverflow上有一个类似的问题: Python super()引发TypeError ,其中错误的解释是用户类不是新式类。 但是,上面的类是一个新式类,因为它继承自object
:
>>> isinstance(HTMLParser(), object)
True
我错过了什么? 我怎么能在这里使用super()
?
使用HTMLParser.__init__(self)
而不是super(TextParser, self).__init__()
可以工作,但我想了解TypeError。
PS:Joachim指出,作为一个新式的实例并不等同于成为一个object
。 我多次反复阅读,因此我的困惑(基于object
实例测试的新式类实例测试示例: https : //stackoverflow.com/revisions/2655651/3 )。
好吧,这是通常的“ super()
不能用于旧式的”。
然而,重要的一点是正确的测试 “这是一个新风格的实例 (即对象)?” 是
>>> class OldStyle: pass
>>> instance = OldStyle()
>>> issubclass(instance.__class__, object)
False
而不是(如在问题中):
>>> isinstance(instance, object)
True
对于类 ,正确的“这是一个新式的类”测试是:
>>> issubclass(OldStyle, object) # OldStyle is not a new-style class
False
>>> issubclass(int, object) # int is a new-style class
True
关键点在于,对于旧式类,实例的类及其类型是不同的。 这里, OldStyle().__class__
是OldStyle
,其不从继承object
,而type(OldStyle())
是instance
类型, 它从继承object
。 基本上,旧式类只创建类型instance
对象(而新式类创建类型为类本身的对象)。 这大概就是为什么实例OldStyle()
是一个object
:它的type()
从继承object
(事实上,它的类没有从继承object
:老式类只是构建类型的新对象不计算instance
)。 部分参考: https : //stackoverflow.com/a/9699961/42973 。
PS:新风格类和旧风格类之间的区别也可以通过以下方式看出:
>>> type(OldStyle) # OldStyle creates objects but is not itself a type
classobj
>>> isinstance(OldStyle, type)
False
>>> type(int) # A new-style class is a type
type
(旧式类不是类型,因此它们不能是它们实例的类型)。
super()只能在new-style类中使用,这意味着root类需要从'object'类继承。
例如,顶级类需要像这样:
class SomeClass(object):
def __init__(self):
....
不
class SomeClass():
def __init__(self):
....
所以,解决方案是直接调用父的init方法,就像这样:
class TextParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.all_data = []
您还可以使用class TextParser(HTMLParser, object):
这使得TextParser
成为一种新式的类,并且可以使用super()
。
问题是super
需要一个object
作为祖先:
>>> class oldstyle:
... def __init__(self): self.os = True
>>> class myclass(oldstyle):
... def __init__(self): super(myclass, self).__init__()
>>> myclass()
TypeError: must be type, not classobj
仔细研究后发现:
>>> type(myclass)
classobj
但:
>>> class newstyle(object): pass
>>> type(newstyle)
type
因此,您的问题的解决方案是从对象以及HTMLParser继承。 但要确保对象在MRO类中排在最后:
>>> class myclass(oldstyle, object):
... def __init__(self): super(myclass, self).__init__()
>>> myclass().os
True
如果查看继承树(在2.6版本中), HTMLParser
继承自从ParserBase
继承的SGMLParser
,它不从object
继承。 即HTMLParser是一个旧式的类。
关于你的isinstance
,我在ipython中做了一个快速测试:
In [1]: class A: ...: pass ...: In [2]: isinstance(A, object) Out[2]: True
即使一个类是旧式类,它仍然是一个object
的实例。
正确的方法是在旧式类中继承“对象”
class A:
def foo(self):
return "Hi there"
class B(A):
def foo(self, name):
return A.foo(self) + name
FWIW虽然我不是Python大师,但我还是接受了这个
>>> class TextParser(HTMLParser):
... def handle_starttag(self, tag, attrs):
... if tag == "b":
... self.all_data.append("bold")
... else:
... self.all_data.append("other")
...
...
>>> p = TextParser()
>>> p.all_data = []
>>> p.feed(text)
>>> print p.all_data
(...)
刚刚根据需要解析了解析结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.