天天看点

对于新式类,super()引发“TypeError:必须是type,而不是classobj”

本文翻译自:super() raises “TypeError: must be type, not classobj” for new-style class

The following use of

super()

raises a TypeError: why?

以下使用

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
           

There is a similar question on StackOverflow: Python super() raises TypeError , where the error is explained by the fact that the user class is not a new-style class.

StackOverflow上有一个类似的问题: Python super()引发TypeError ,其中错误的解释是用户类不是新式类。

However, the class above is a new-style class, as it inherits from

object

:

但是,上面的类是一个新式类,因为它继承自

object

>>> isinstance(HTMLParser(), object)
True
           

What am I missing?

我错过了什么?

How can I use

super()

, here?

我怎么能在这里使用

super()

Using

HTMLParser.__init__(self)

instead of

super(TextParser, self).__init__()

would work, but I would like to understand the TypeError.

使用

HTMLParser.__init__(self)

而不是

super(TextParser, self).__init__()

可以工作,但我想了解TypeError。

PS: Joachim pointed out that being a new-style-class instance is not equivalent to being an

object

.

PS:Joachim指出,作为一个新式的实例并不等同于成为一个

object

I read the opposite many times, hence my confusion (example of new-style class instance test based on

object

instance test: https://stackoverflow.com/revisions/2655651/3 ).

我多次反复阅读,因此我的困惑(基于

object

实例测试的新式类实例测试示例: https : //stackoverflow.com/revisions/2655651/3 )。

#1楼

参考:https://stackoom.com/question/eh3G/对于新式类-super-引发-TypeError-必须是type-而不是classobj

#2楼

You can also use

class TextParser(HTMLParser, object):

.

您还可以使用

class TextParser(HTMLParser, object):

This makes

TextParser

a new-style class, and

super()

can be used.

这使得

TextParser

成为一种新式的类,并且可以使用

super()

#3楼

the correct way to do will be as following in the old-style classes which doesn't inherit from 'object'

正确的方法是在旧式类中继承“对象”
class A:
    def foo(self):
        return "Hi there"

class B(A):
    def foo(self, name):
        return A.foo(self) + name
           

#4楼

The problem is that

super

needs an

object

as an ancestor:

问题是

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
           

On closer examination one finds:

仔细研究后发现:
>>> type(myclass)
classobj
           

But:

但:
>>> class newstyle(object): pass

>>> type(newstyle)
type    
           

So the solution to your problem would be to inherit from object as well as from HTMLParser.

因此,您的问题的解决方案是从对象以及HTMLParser继承。

But make sure object comes last in the classes MRO:

但要确保对象在MRO类中排在最后:
>>> class myclass(oldstyle, object):
...     def __init__(self): super(myclass, self).__init__()

>>> myclass().os
True
           

#5楼

FWIW and though I'm no Python guru I got by with this

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
(...)
           

Just got me the parse results back as needed.

刚刚根据需要解析了解析结果。

#6楼

If you look at the inheritance tree (in version 2.6),

HTMLParser

inherits from

SGMLParser

which inherits from

ParserBase

which doesn't inherits from

object

.

如果查看继承树(在2.6版本中),

HTMLParser

继承自从

ParserBase

继承的

SGMLParser

,它不从

object

继承。

Ie HTMLParser is an old-style class.

即HTMLParser是一个旧式的类。

About your checking with

isinstance

, I did a quick test in ipython:

关于你的

isinstance

,我在ipython中做了一个快速测试:
In [1]: class A:
   ...:     pass
   ...: 

In [2]: isinstance(A, object)
Out[2]: True      

Even if a class is old-style class, it's still an instance of

object

.

即使一个类是旧式类,它仍然是一个

object

的实例。