Inheritance (Python)

Inheritance lets one class (the child) reuse and extend the attributes and methods of another class (the parent). You write it by putting the parent class in parentheses after the child's name.

Python

The child class can add new methods or override existing ones. Use super() to call the parent's version of a method, such as its __init__.

Syntax

class Child(Parent):

Examples

Python

class Animal:
    def speak(self):
        return "..."

class Cat(Animal):
    def speak(self):
        return "meow"

print(Cat().speak())

Output:

meow

Learn it in a lesson

See also

Back to the Dictionary