Can a class inherit from more than one base class?


#1

Question

In this exercise, a standalone class exists as well as another class that inherits from it. Is it possible to inherit from more than one class?

Answer

Yes, a class can inherit from more than one base class. The term for this is “multiple inheritance”. When inheriting from a single class, the base class is included in parenthesis as part of the class definition following the class name. To inherit from additional base classes, classes are added into the definition separated by commas. An example of multiple inheritance is shown in the code below.

class A:
    pass

class B:
    pass

class C(A,B):
    pass