How do you inherit one class from another class in Python?

Experience Level: Junior
Tags: Python

Answer

To inherit one class from another, define a new class and add the class name of the class to inherit from between brackets after the first class name.

Example:

class Parent:
    def do_something():
        print("Called do_something()")


class Child(Parent):
    def do_something_else():
        print("Called do_something()")


value = Child()
value.do_something()
value.do_something_else()

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself