{"id":172,"date":"2024-01-25T14:44:47","date_gmt":"2024-01-25T14:44:47","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=172"},"modified":"2024-01-25T14:44:49","modified_gmt":"2024-01-25T14:44:49","slug":"python-inheritance","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-inheritance\/","title":{"rendered":"Python Inheritance"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python inheritance and how to use the inheritance to reuse code from an existing class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python inheritance<\/h2>\n\n\n\n<p>Inheritance allows a\u00a0class\u00a0to reuse the logic of an existing class. Suppose you have the following\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name): self.name = name def greet(self): return f\"Hi, it's {self.name}\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>Person<\/code>&nbsp;class has the&nbsp;<code>name<\/code>&nbsp;attribute and the&nbsp;<code>greet()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Now, you want to define the\u00a0<code>Employee<\/code>\u00a0that is similar to the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee: def __init__(self, name, job_title): self.name = name self.job_title = job_title def greet(self): return f\"Hi, it's {self.name}\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>Employee<\/code>&nbsp;class has two attributes&nbsp;<code>name<\/code>&nbsp;and&nbsp;<code>job_title<\/code>. It also has the&nbsp;<code>greet()<\/code>&nbsp;method that is exactly the same as the&nbsp;<code>greet()<\/code>&nbsp;method of the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>To reuse the&nbsp;<code>greet()<\/code>&nbsp;method from the&nbsp;<code>Person<\/code>&nbsp;class in the&nbsp;<code>Employee<\/code>&nbsp;class, you can create a relationship between the&nbsp;<code>Person<\/code>&nbsp;and&nbsp;<code>Employee<\/code>&nbsp;classes. To do it, you use inheritance so that the&nbsp;<code>Employee<\/code>&nbsp;class inherits from the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>The following redefines the\u00a0<code>Employee<\/code>\u00a0class that inherits from the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee(Person): def __init__(self, name, job_title): self.name = name self.job_title = job_title<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>By doing this, the&nbsp;<code>Employee<\/code>&nbsp;class behaves the same as the&nbsp;<code>Person<\/code>&nbsp;class without redefining the greet() method.<\/p>\n\n\n\n<p>This is a&nbsp;<strong>single inheritance<\/strong>&nbsp;because the&nbsp;<code>Employee<\/code>&nbsp;inherits from a single class (<code>Person<\/code>). Note that Python also supports multiple inheritances where a class inherits from multiple classes.<\/p>\n\n\n\n<p>Since the&nbsp;<code>Employee<\/code>&nbsp;inherits attributes and methods of the&nbsp;<code>Person<\/code>&nbsp;class, you can use the instance of the Employee class as if it were an instance of the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>For example, the following creates a new instance of the\u00a0<code>Employee<\/code>\u00a0class and call the\u00a0<code>greet()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>employee = Employee('John', 'Python Developer') print(employee.greet())<\/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>Hi, it's John<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Inheritance terminology<\/h3>\n\n\n\n<p>The&nbsp;<code>Person<\/code>&nbsp;class is the&nbsp;<strong>parent class<\/strong>, the&nbsp;<strong>base class<\/strong>, or the&nbsp;<strong>super class<\/strong>&nbsp;of the&nbsp;<code>Employee<\/code>&nbsp;class. And the&nbsp;<code>Employee<\/code>&nbsp;class is a&nbsp;<strong>child&nbsp;<\/strong>class, a&nbsp;<strong>derived class<\/strong>, or a&nbsp;<strong>subclass&nbsp;<\/strong>of the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>The&nbsp;<code>Employee<\/code>&nbsp;class&nbsp;<strong>derives from<\/strong>,&nbsp;<strong>extends<\/strong>, or&nbsp;<strong>subclasses&nbsp;<\/strong>the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>The relationship between the&nbsp;<code>Employee<\/code>&nbsp;class and&nbsp;<code>Person<\/code>&nbsp;class is&nbsp;<strong>IS-A<\/strong>&nbsp;relationship. In other words, an employee&nbsp;<strong>is a<\/strong>&nbsp;person.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">type vs. isinstance<\/h2>\n\n\n\n<p>The following shows the type of instances of the\u00a0<code>Person<\/code>\u00a0and\u00a0<code>Employee<\/code>\u00a0classes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('Jane') print(type(person)) employee = Employee('John', 'Python Developer') print(type(employee))<\/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__.Person'> &lt;class '__main__.Employee'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To check if an object is an instance of a class, you use the\u00a0<code>isinstance()<\/code>\u00a0method. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('Jane') print(isinstance(person, Person)) <em># True<\/em> employee = Employee('John', 'Python Developer') print(isinstance(employee, Person)) <em># True<\/em> print(isinstance(employee, Employee)) <em># True<\/em> print(isinstance(person, Employee)) <em># False<\/em><\/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>True True True False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>As clearly shown in the output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>person<\/code>&nbsp;is an instance of the&nbsp;<code>Person<\/code>&nbsp;class.<\/li>\n\n\n\n<li>The&nbsp;<code>employee<\/code>&nbsp;is an instance of the&nbsp;<code>Employee<\/code>&nbsp;class. It\u2019s also an instance of the&nbsp;<code>Person<\/code>&nbsp;class.<\/li>\n\n\n\n<li>The&nbsp;<code>person<\/code>&nbsp;is not an instance of the&nbsp;<code>Employee<\/code>&nbsp;class.<\/li>\n<\/ul>\n\n\n\n<p>In practice, you\u2019ll often use the&nbsp;<code>isinstance()<\/code>&nbsp;function to check whether an object has certain methods. Then, you\u2019ll call the methods of that object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">issubclass<\/h3>\n\n\n\n<p>To check if a class is a subclass of another class, you use the\u00a0<code>issubclass()<\/code>\u00a0function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(issubclass(Employee, Person)) <em># True<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following defines the\u00a0<code>SalesEmployee<\/code>\u00a0class that inherits from the\u00a0<code>Employee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class SalesEmployee(Employee): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>SalesEmployee<\/code>\u00a0is the subclass of the\u00a0<code>Employee<\/code>\u00a0class. It\u2019s also a subclass of the\u00a0<code>Person<\/code>\u00a0class as shown in the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(issubclass(SalesEmployee, Employee)) <em># True<\/em> print(issubclass(SalesEmployee, Person)) <em># True<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that when you define a class that doesn\u2019t inherit from any class, it\u2019ll implicitly inherit from the built-in&nbsp;<code>object<\/code>&nbsp;class.<\/p>\n\n\n\n<p>For example, the\u00a0<code>Person<\/code>\u00a0class inherits from the\u00a0<code>object<\/code>\u00a0class implicitly. Therefore, it is a subclass of the\u00a0<code>object<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(issubclass(Person, object)) <em># True<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In other words, all classes are subclasses of the&nbsp;<code>object<\/code>&nbsp;class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python inheritance and how to use the inheritance to reuse code from an existing class. Introduction to the Python inheritance Inheritance allows a\u00a0class\u00a0to reuse the logic of an existing class. Suppose you have the following\u00a0Person\u00a0class: The&nbsp;Person&nbsp;class has the&nbsp;name&nbsp;attribute and the&nbsp;greet()&nbsp;method. Now, you want to define the\u00a0Employee\u00a0that is similar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-4-single-inheritance"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/172","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=172"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/172\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}