Superclass __init__ overrides subclass constructor

113 views Asked by At

I am working with cantera, trying to create an expansion of cantera.Solution class by creating a subclass Flow. This is basically to expand description of gas phase with parameters of its movement.

Solution is constructed by using certain **kwargs. What I am trying to do, is to hold those **kwargs in a Template class object and use that object to define my Flow with that. I was trying to shadow Solution.__init__ with Flow.__init__ and pass parameters from Template using super(). However, Solution.__init__ seems to override Flow.__init__.

class Flow(Solution):

  def __init__(self, template, velocity):

    super().__init__(species = template.species,
                     reactions = template.reactions, 
                     thermo = template.thermo, 
                     kinetics = 'GasKinetics')

    self.velocity = velocity

Now, let's assume i have a proper Template object under the name of template, which serves as a container for all those **kwargs required by Solution.__init__. I'm trying to create my Flow object:

flow = Flow(template, 230)

And i get:

AttributeError: 'Template' object has no attribute 'encode'

The very same error I would get if I attempted:

S = Solution(template, 230)

So basically all my subclass constructor parameters are passed to superclass' constructor. As the superclass constructor is not overridden, I can not use Template objects to define my Solution as a base for Flow. I read all around the web that this is not the default behaviour, as subclass constructor should override superclass constructor. How can this be helped?

1

There are 1 answers

0
AnacondaPython On

I have found a solution. Turns out, Cantera is based around cython. In fact, Solution class contains init, but it's not seen by Python as a function, but as a slot wrapper. Because of this, subclasses are unable to shadow init with their own constructor. I have handled this elegantly using unmentioned intestines of my code.