Can a class inherit from more than one base class?

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
8 Likes

Does that mean it also inherits all of the parent classes’ constructor functions? How would one pass in arguments when instantiating a child class with multiple parents, by passing in the arguments in the order of the child class’ own arguments?

2 Likes

Hi @sam_the_jam,

This page has lots of documentation about Python classes: Classes

See this section: Python: Classes: Multiple Inheritance

Here’s a quote from that section:

For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy. Thus, if an attribute is not found in DerivedClassName , it is searched for in Base1 , then (recursively) in the base classes of Base1 , and if it was not found there, it was searched for in Base2 , and so on.

In fact, it is slightly more complex than that; the method resolution order changes dynamically to support cooperative calls to super() . This approach is known in some other multiple-inheritance languages as call-next-method and is more powerful than the super call found in single-inheritance languages.

A depth-first, left-to-right search for attributes consists of a search of each parent class, from the left-most to the right-most of the listings of parents in the class header, until the attribute is found. Methods are considered to be attributes. Once the attribute is found for the first time, the search ends. The search is recursive, so if any of the parent classes inherits from multiple parents, the depth-first, left-to-right search includes the parents of the parent classes.

Following is a simple example where the __init__ method is found in class A, so that method is inherited by class C:

class A:
  def __init__(self):
    self.name = "class A"

class B:
  def __init__(self):
    self.name = "class B"

class C(A, B):
  pass

c = C()
print(c.name)

Output:

class A

EDITED on September 20, 2019 to provide the following example:

Here’s a more complex example where, due to the search order, class Z inherits __init__ from class X, and show_color from class Y:

class X:
  def __init__(self, width, height, depth, color):
    self.width = width
    self.height = height
    self.depth = depth
    self.color = color

class Y:
  def __init__(self, width, height, color):
    self.width = width
    self.height = height
    self.color = color
  def show_color(self):
    return "I am {}.".format(self.color)

class Z(X, Y):
  pass

z = Z(2, 3, 4, "green")
print(z.show_color())

Output:

I am green.

To know what arguments to pass to the __init__ method, we needed to figure out which __init__ method was inherited.

To gain more insight, you can experiment with various configurations.

13 Likes

this could become infinitely complex, Is JS single inheritance or multiple inheritance languge ?

1 Like

Hi @css7713845194,

JavaScript uses single inheritance.

See MDN: Web technology for developers > JavaScript > Inheritance and the prototype chain.

There’s a lot to read there. Note this:

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype . That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain .

Note that in the prototype chain, an object can only have one direct prototype.

3 Likes

Many thanks for the answer. Could you please raise some examples? Thanks again!