4

Perhaps this is a silly question, but why doesn't this code work in python 2.7?

from ConfigParser import ConfigParser

class MyParser(ConfigParser):
    def __init__(self, cpath):
        super(MyParser, self).__init__()
        self.configpath = cpath
        self.read(self.configpath)

It fails on:

TypeError: must be type, not classobj

on the super() line.

lollercoaster
  • 15,969
  • 35
  • 115
  • 173

2 Answers2

5

Most likely because ConfigParser does not inherit from object, hence, is not a new-style class. That's why super doesn't work there.

Check the ConfigParser definition and verify if it's like this:

class ConfigParser(object): # or inherit from some class who inherit from object

If not, that's the problem.

My advice to your code is not use super. Just invoke directly self on the ConfigParser like this:

class MyParser(ConfigParser):
    def __init__(self, cpath):
        ConfigParser.__init__(self)
        self.configpath = cpath
        self.read(self.configpath)
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
4

The problem is that ConfigParser is an old-style class. super does not work with old-style classes. Instead, use explicit call to __init__:

def __init__(self, cpath):
     ConfigParser.__init__(self)
     self.configpath = cpath
     self.read(self.configpath)

See this question, for example, for an explanation of new vs old style classes.

Community
  • 1
  • 1
shx2
  • 61,779
  • 13
  • 130
  • 153