Reputation: 7699
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)
Upvotes: 1
Views: 899
Reputation: 78790
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
.
Upvotes: 3