3

I have base class like below

class FileUtil:
    def __init__(self):
        self.outFileDir = os.path.join(settings.MEDIA_ROOT,'processed')
        if not os.path.exists(outFileDir):
            os.makedirs(outFileDir)
    ## other methods of the class

and I am extending this class as below:

class Myfile(FileUtil):
    def __init__(self, extension):
        super(Myfile, self).__init__()
        self.extension = 'text'
    ## other methods of class

But i am getting below error?

super(Myfile, self).__init__()
TypeError: super() takes at least 1 argument (0 given)

I gone through many documents and found that there are different sytex of calling super() in 2.x and 3.x. I tried both ways but getting error.

Gaurav Pant
  • 4,029
  • 6
  • 31
  • 54

2 Answers2

8

You have 2 options

old style class, you should call the super constructor directly.

class FileUtil():
    def __init__(self):
        pass

class Myfile(FileUtil):
    def __init__(self, extension):
        FileUtil.__init__(self)

new style class, inherit from object in your base class and your current call to super will be processed correctly.

class FileUtil(object):
    def __init__(self):
        pass

class Myfile(FileUtil):
    def __init__(self, extension):
        super(Myfile, self).__init__()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0

You may need to create the FileUtil class using the super() function as well:

class FileUtil(object):
    def __init__(self):
        super(FileUtil, self).__init__()
        ...
tjohnson
  • 1,047
  • 1
  • 11
  • 18