Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (7.3k points)

The following use of super() raises a TypeError: why?

>>> from HTMLParser import HTMLParser

>>> class TextParser(HTMLParser):

... def __init__(self):

... super(TextParser, self).__init__() 

... self.all_data = [] 

... 

>>> TextParser()

(...)

TypeError: must be type, not classobj

What am I missing? How can I use super(), here?

Using HTMLParser.__init__(self) instead of super(TextParser, self).__init__() would work, but I would like to understand the TypeError.

1 Answer

0 votes
by (106k points)

To remove the above Type error that you are getting while using the super(). The super() can only be used in the new-style classes, that means the root or the parent  class needs to inherit from the 'object' class

An example, that shows how the root class need to be:

class SomeClass(object):

def __init__(self): ....

The solution for this is to call the parent's init method directly like as follows:

from HTMLParser import HTMLParser

class TextParser(HTMLParser):

def __init__(self):

HTMLParser.__init__(self)

self.all_data = []

Browse Categories

...