{"id":204,"date":"2024-01-25T15:44:09","date_gmt":"2024-01-25T15:44:09","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=204"},"modified":"2024-01-25T15:46:39","modified_gmt":"2024-01-25T15:46:39","slug":"python-multiple-inheritance","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-multiple-inheritance\/","title":{"rendered":"Python Multiple Inheritance"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python multiple inheritance and how method order resolution works in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python Multiple inheritance.<\/h2>\n\n\n\n<p>When a&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a>&nbsp;inherits from a single class, you have&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">single inheritance<\/a>. Python allows a class to inherit from multiple classes. If a class inherits from two or more classes, you\u2019ll have multiple inheritance.<\/p>\n\n\n\n<p>To extend multiple classes, you specify the parent classes inside the parentheses&nbsp;<code>()<\/code>&nbsp;after the class name of the child class like this:<code>class ChildClass(ParentClass1, ParentClass2, ParentClass3): pass<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>The syntax for multiple inheritance is similar to a parameter list in the class definition. Instead of including one parent class inside the parentheses, you include two or more classes, separated by a comma.<\/p>\n\n\n\n<p>Let\u2019s take an example to understand how multiple inheritance works:<\/p>\n\n\n\n<p>First, define a class\u00a0<code>Car<\/code>\u00a0that has the\u00a0<code>go()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Car: def go(self): print('Going')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, define a class\u00a0<code>Flyable<\/code>\u00a0that has the\u00a0<code>fly()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Flyable: def fly(self): print('Flying')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Third, define the\u00a0<code>FlyingCar<\/code>\u00a0that inherits from both\u00a0<code>Car<\/code>\u00a0and\u00a0<code>Flyable<\/code>\u00a0classes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class FlyingCar(Flyable, Car): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since the\u00a0<code>FlyingCar<\/code>\u00a0inherits from\u00a0<code>Car<\/code>\u00a0and\u00a0<code>Flyable<\/code>\u00a0classes, it reuses the methods from those classes. It means you can call the\u00a0<code>go()<\/code>\u00a0and\u00a0<code>fly()<\/code>\u00a0methods on an instance of the\u00a0<code>FlyingCar<\/code>\u00a0class like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if __name__ == '__main__': fc = FlyingCar() fc.go() fc.fly()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Going Flying<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method resolution order (MRO)<\/h2>\n\n\n\n<p>When the parent classes have methods with the same name and the child class calls the method, Python uses the method resolution order (MRO) to search for the right method to call. Consider the following example:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/Python-Multiple-Inhertiance-MRO.png\" alt=\"\" class=\"wp-image-2746\"\/><\/figure>\n\n\n\n<p>First, add the\u00a0<code>start()<\/code>\u00a0method to the\u00a0<code>Car<\/code>,\u00a0<code>Flyable<\/code>, and\u00a0<code>FlyingCar<\/code>\u00a0classes. In the\u00a0<code>start()<\/code>\u00a0method of the\u00a0<code>FlyingCar<\/code>\u00a0class, call the\u00a0<code>start()<\/code>\u00a0method of the\u00a0<code>super()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Car: def start(self): print('Start the Car') def go(self): print('Going') class Flyable: def start(self): print('Start the Flyable object') def fly(self): print('Flying') class FlyingCar(Flyable, Car): def start(self): super().start()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, create an instance of the\u00a0<code>FlyingCar<\/code>\u00a0class and call the\u00a0<code>start()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if __name__ == '__main__': car = FlyingCar() car.start()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Start the Flyable object<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>As you can see clearly from the output, the&nbsp;<code>super().start()<\/code>&nbsp;calls the&nbsp;<code>start()<\/code>&nbsp;method of the&nbsp;<code>Flyable<\/code>&nbsp;class.<\/p>\n\n\n\n<p>The following shows the\u00a0<code>__mro__<\/code>\u00a0of the\u00a0<code>FlyingCar<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(FlyingCar.__mro__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>(&lt;class '__main__.FlyingCar'>, &lt;class '__main__.Flyable'>, &lt;class '__main__.Car'>, &lt;class 'object'>)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>From left to right, you\u2019ll see the&nbsp;<code>FlyingCar<\/code>,&nbsp;<code>Flyable<\/code>,&nbsp;<code>Car<\/code>, and&nbsp;<code>object<\/code>.<\/p>\n\n\n\n<p>Note that the&nbsp;<code>Car<\/code>&nbsp;and&nbsp;<code>Flyable<\/code>&nbsp;objects inherit from the&nbsp;<code>object<\/code>&nbsp;class implicitly. When you call the&nbsp;<code>start()<\/code>&nbsp;method from the&nbsp;<code>FlyingCar<\/code>\u2018s object, Python uses the&nbsp;<code>__mro__<\/code>&nbsp;class search path.<\/p>\n\n\n\n<p>Since the&nbsp;<code>Flyable<\/code>&nbsp;class is next to the&nbsp;<code>FlyingCar<\/code>&nbsp;class, the&nbsp;<code>super().start()<\/code>&nbsp;calls the&nbsp;<code>start()<\/code>&nbsp;method of the&nbsp;<code>FlyingCar<\/code>&nbsp;class.<\/p>\n\n\n\n<p>If you flip the order of\u00a0<code>Flyable<\/code>\u00a0and\u00a0<code>Car<\/code>\u00a0classes in the list, the\u00a0<code>__mro__<\/code>\u00a0will change accordingly. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># Car, Flyable classes...<\/em> class FlyingCar(Car, Flyable): def start(self): super().start() if __name__ == '__main__': car = FlyingCar() car.start() print(FlyingCar.__mro__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Start the Car (&lt;class '__main__.FlyingCar'>, &lt;class '__main__.Car'>, &lt;class '__main__.Flyable'>, &lt;class 'object'>)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>super().start()<\/code>&nbsp;calls the&nbsp;<code>start()<\/code>&nbsp;method of the&nbsp;<code>Car<\/code>&nbsp;class instead, based on their orders in the method order resolution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple inheritance &amp; super<\/h2>\n\n\n\n<p>First, add the\u00a0<code>__init__<\/code>\u00a0method to the\u00a0<code>Car<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Car: def __init__(self, door, wheel): self.door = door self.wheel = wheel def start(self): print('Start the Car') def go(self): print('Going')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, add the\u00a0<code>__init__<\/code>\u00a0method to the\u00a0<code>Flyable<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Flyable: def __init__(self, wing): self.wing = wing def start(self): print('Start the Flyable object') def fly(self): print('Flying')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>__init__<\/code>&nbsp;of the&nbsp;<code>Car<\/code>&nbsp;and&nbsp;<code>Flyable<\/code>&nbsp;classes accept a different number of parameters. If the&nbsp;<code>FlyingCar<\/code>&nbsp;class inherits from the&nbsp;<code>Car<\/code>&nbsp;and&nbsp;<code>Flyable<\/code>&nbsp;classes, its&nbsp;<code>__init__<\/code>&nbsp;method needs to call the right&nbsp;<code>__init__<\/code>&nbsp;method specified in the method order resolution&nbsp;<code>__mro__<\/code>&nbsp;of the&nbsp;<code>FlyingCar<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Third, add the\u00a0<code>__init__<\/code>\u00a0method to the\u00a0<code>FlyingCar<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class FlyingCar(Flyable, Car): def __init__(self, door, wheel, wing): super().__init__(wing=wing) self.door = door self.wheel = wheel def start(self): super().start()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The method order resolution of the\u00a0<code>FlyingCar<\/code>\u00a0class is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>(&lt;class '__main__.FlyingCar'>, &lt;class '__main__.Flyable'>, &lt;class '__main__.Car'>, &lt;class 'object'>)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>the&nbsp;<code>super().__init__()<\/code>&nbsp;calls the&nbsp;<code>__init__<\/code>&nbsp;of the&nbsp;<code>FlyingCar<\/code>&nbsp;class. Therefore, you need to pass the&nbsp;<code>wing<\/code>&nbsp;argument to the&nbsp;<code>__init__<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Because the&nbsp;<code>FlyingCar<\/code>&nbsp;class cannot access the&nbsp;<code>__init__<\/code>&nbsp;method of the&nbsp;<code>Car<\/code>&nbsp;class, you need to initialize the&nbsp;<code>door<\/code>and&nbsp;<code>wheel<\/code>&nbsp;attributes individually.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python multiple inheritance and how method order resolution works in Python. Introduction to the Python Multiple inheritance. When a&nbsp;class&nbsp;inherits from a single class, you have&nbsp;single inheritance. Python allows a class to inherit from multiple classes. If a class inherits from two or more classes, you\u2019ll have multiple inheritance. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-7-multiple-inheritance-descriptors"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/204\/revisions\/205"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}