Inheritance

Updated on 29 Dec 2022

You can inherit from another class using this syntax.

class Cat(Animal):
    ...

Calling Parent Method

There are two techniques.

You can call a parent method using the super syntax.

def myFunction(self):
    super().myFunction()

Or call it using the parent classname. In the example below, I have MyChildClass inheriting from MyParentClass. You still need to pass self to the method call.

def myFunction(self):
    MyParentClass.myFunction(self)

Example

class MyParentClass:

    def __init__(self):
        self.blahblah = "blahblah"

    def myFunction(self):
        print("parent - " + self.blahblah)

class MyChildClass(MyParentClass):
    def myFunction(self):
        print("child - " + self.blahblah)
        MyParentClass.myFunction(self)
        super().myFunction()

myObject = MyChildClass()
myObject.myFunction()