Home > Blockchain >  Using an inherit method to extend its functionality
Using an inherit method to extend its functionality

Time:12-16

I am trying to extend a method doing the following:

`

class Parent:
    def extract(self):
        #code

class Child(Parent):
    def extract(self):
        self.extract()
        self.parse()
    
    def parse(self):
        #code

` I do not know if this is the right way to do it but my kernel explodes when I try to do this with my code. What I want to achieve is to use the code from the parent class new code from the child class knowing that the parent class will be inherited by other classes that would not need to have the extract method extended.

CodePudding user response:

The problem is you are trying to access the base class one of function, but you already override into the child class. You can do it using with this way;

class Parent:
    def extract(self):
        print("base class extract")


class Child(Parent):
    def extract(self):
        super(Child, self).extract()
        self.parse()

    def parse(self):
        print("child class parse")


if __name__ == '__main__':
    sample_field = Child()
    sample_field.extract()

# output:
# base class extract
# child class parse

  • Related