Search code examples
pythonoopsuper

How to use the method of base class in the subclass of derived class?


I need the python to use the method of base class in the below program. I read lots of documentation, even after that also, I couldn't able to figure it out how super works when the below kind of situation occurs.

class Car:
    time = 2
    def __init__(self, color, company,powertrain,velocity):
        self.color = color
        self.company = company
        self.powertrain = powertrain
        self.velocity = velocity

    def speed(self):
        return self.velocity

    def __self__(self):
        return self.powertrain   #just self.powertrain to print it

class Conventional(Car):

    def speed(self):

        return self.velocity *self.time*2

class Electric(Car):

    def __init__(self):
        super().__init__()

    def speed(self):
        return super().velocity * super().time*3

 class Hybrid(Electric,Conventional):

    pass

Now I need hybrid object to use the method of Car class, I think I can use composition concept here. But I need to know how super works here. I also know mro to get the order in which python looks for the method. mro.

car = Hybrid("blue", "Audi", "hyrid", 50)
car.speed()

Any better way to write the above code is most welcomed. I think if I get the solution to the above problem using super (), I can understand the complete functionality of super properly. To see all the possibilities I Used different syntax. Don't mind

Thanks in advance


Solution

  • This should work:

    super(Electric, Electric).speed(self, velocity)
    

    Explanation: super can take two optional arguments, the superclass (direct superclass) it should use, and class that it should start searching through MRO from.: MRO: Hybrid -> Electric -> Car -> Conventional -> Car By passing Electric we make MRO: Car -> Conventional -> Car (multiple inheritance is awful) When we resolve inheritance like this, we also need to pass the current object explicitly (self). Btw, you should fix your inits, they dont resolve propely, you should always pass down the *args, and let the super resolve arguments for you.

    class Car:
        time = 2
        def __init__(self, color, company,powertrain,velocity):
            self.color = color
            self.company = company
            self.powertrain = powertrain
            self.velocity = velocity
    
        def speed(self, velocity):
            return self.velocity
    
        def __self__(self):
            return self.powertrain   #just self.powertrain to print it
    
    class Conventional(Car):
        def __init__(self,*args):
            super().__init__(*args)
    
    
        def speed(self):
    
            return self.velocity *self.time*2
    
    class Electric(Car):
    
        def __init__(self,*args):
            super().__init__(*args)
    
        def speed(self):
            return super().velocity * super().time*3
    
    class Hybrid(Electric,Conventional):
        def __init__(self,*args):
            super().__init__(*args)
    
        def speed(self):
            return super(Electric, Electric
                    ).speed(self,50)
    
    car = Hybrid("blue", "Audi", "hyrid", 50)
    car.speed()