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()
Related Python job interview questions
What is bool data type good for in Python?
Python JuniorWhat is a Set data type good for in Python?
Python JuniorWhy would you want to inherit a class for another class in Python?
Python JuniorWhat does strftime stand for in Python?
Python JuniorWhat does strptime stand for?
Python Junior