1

I am trying to inherit a class constructor from a parent class. After trying several of the suggestions already on stack overflow I thought I'd ask a question myself to try and understand 1) why this code is wrong and 2) how to correct it?

The parent class:

class Submit_Copasi_Job(object):
    '''
    Submit a properly formatted copasi file to sun grid engine
    '''
    def __init__(self,copasi_file,report_name):
        self.copasi_file=copasi_file
        self.copasiML_str=self._read_copasiML_as_string()
        self.report_name=report_name
        self.submit_copasi_job_SGE()

    def _read_copasiML_as_string(self):
        '''
        Read a copasiML file as string 
        '''
        assert os.path.exists(self.copasi_file), "{} does not exist!".format(self.copasi_file)
        with open(self.copasi_file) as f:
            fle = f.read()
        return fle

        ....

The child class (which tries to use the super(SubClass, self).__init__(...) but obviously I have something wrong)

class Submit_Copasi_Multijob(Submit_Copasi_Job):
    def __init__(self):
        super(Submit_Copasi_Multijob,self).__init__(copasi_file,report_name)

    def test(self):
        return self.copasi_file

Run code

fle='D:\\MPhil\\Model_Building\\Models\\TGFB\\Fli1_Models\\M7.cps'
s=Submit_Copasi_Multijob(fle,'rep.txt')
print s.test()

All my attempts so far have resulted in a similar error:

   s=Submit_Copasi_Multijob(fle,'rep')

TypeError: __init__() takes exactly 1 argument (3 given)
2
  • 2
    Look at the __init__ method of Submit_Copasi_Multijob... where are copasi_file and report_name defined? You forgot to add the arguments. Commented Jan 24, 2016 at 13:50
  • @Bakuriu Yes, that was it. Thanks. If you write an answer below I'll accept. Commented Jan 24, 2016 at 13:55

1 Answer 1

3

The problem is that the __init__ method of your child class only takes one argument, which will automatically be supplied by Python as a reference to the newly created object. So when you instantiate a Submit_Copasi_Multijob object, you cannot supply any arguments, i.e. the way your code is currently written the correct way to instantiate an object of your child class is

new_obj = Submit_Copasi_Multijob()

Of course, this will fail while executing __init__ because the names copasi_file and report_name won't be defined.

Fix this by letting the __init__ method of your child-class take three arguments instead of one:

def __init__(self, compasi_file, report_name):
    super(Submit_Copasi_Multijob,self).__init__(copasi_file,report_name)
    # other stuff

However, if this is the only thing you ever plan to do in the __init__ method of your child-class (i.e. there's no # other stuff), there's no need to override the __init__ method of your base class. In that case, just omit the definition of __init__ in Submit_Copasi_Multijob.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi timgeb, just to clarify, if I only had the copasi_file and report_name arguments in my subclass I do not need a constructor at all?
@user3059024 yes, depending on what you want to do. If you omit the definition of __init__ in your sublass, it will inherit the __init__ from the base class. If the __init__ method of the subclass is supposed to do exactly the same thing as the __init__ method of the base class, then there's no need to re-define __init__ in the subclass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.