{"id":176,"date":"2024-01-25T14:50:53","date_gmt":"2024-01-25T14:50:53","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=176"},"modified":"2024-01-25T14:50:55","modified_gmt":"2024-01-25T14:50:55","slug":"python-super","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-super\/","title":{"rendered":"Python super"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Python&nbsp;<code>super()<\/code>&nbsp;to delegate to the parent class when overriding methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python super<\/h2>\n\n\n\n<p>First, define an\u00a0<code>Employee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee: def __init__(self, name, base_pay, bonus): self.name = name self.base_pay = base_pay self.bonus = bonus def get_pay(self): return self.base_pay + self.bonus<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Employee<\/code>\u00a0class has three\u00a0instance variables\u00a0<code>name<\/code>,\u00a0<code>base_pay<\/code>, and\u00a0<code>bonus<\/code>. It also has the\u00a0<code>get_pay()<\/code>\u00a0method that returns the total of base_pay and bonus.<\/p>\n\n\n\n<p>Second, define 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): def __init__(self, name, base_pay, bonus, sales_incentive): self.name = name self.base_pay = base_pay self.bonus = bonus self.sales_incentive = sales_incentive def get_pay(self): return self.base_pay + self.bonus + self.sales_incentive<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>SalesEmployee<\/code>\u00a0class has four instance variables\u00a0<code>name<\/code>,\u00a0<code>base_pay<\/code>,\u00a0<code>bonus<\/code>, and\u00a0<code>sales_incentive<\/code>. It has the\u00a0<code>get_pay()<\/code>\u00a0method that\u00a0overrides\u00a0the\u00a0<code>get_pay()<\/code>\u00a0method in the\u00a0<code>Employee<\/code>\u00a0class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">super().__init__()<\/h3>\n\n\n\n<p>The&nbsp;<code>__init__()<\/code>&nbsp;method of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class has some parts that are the same as the ones in the&nbsp;<code>__init__()<\/code>&nbsp;method of the&nbsp;<code>Employee<\/code>&nbsp;class.<\/p>\n\n\n\n<p>To avoid duplication, you can call the&nbsp;<code>__init__()<\/code>&nbsp;method of&nbsp;<code>Employee<\/code>&nbsp;class from the&nbsp;<code>__init__()<\/code>&nbsp;method of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class.<\/p>\n\n\n\n<p>To reference the&nbsp;<code>Employee<\/code>&nbsp;class in the&nbsp;<code>SalesEmployee<\/code>&nbsp;class, you use the&nbsp;<code>super()<\/code>. The&nbsp;<code>super()<\/code>&nbsp;returns a reference of the parent class from a child class.<\/p>\n\n\n\n<p>The following redefines the\u00a0<code>SalesEmployee<\/code>\u00a0class that uses the\u00a0<code>super()<\/code>\u00a0to call the\u00a0<code>__init__()<\/code>\u00a0method of the\u00a0<code>Employee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class SalesEmployee(Employee): def __init__(self, name, base_pay, bonus, sales_incentive): super().__init__(name, base_pay, bonus) self.sales_incentive = sales_incentive def get_pay(self): return self.base_pay + self.bonus + self.sales_incentive<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you create an instance of the\u00a0<code>SalesEmployee<\/code>\u00a0class, Python will execute the\u00a0<code>__init__()<\/code>\u00a0method in the\u00a0<code>SalesEmployee<\/code>\u00a0class. In turn, this\u00a0<code>__init__()<\/code>\u00a0method calls the\u00a0<code>__init__()<\/code>\u00a0method of the\u00a0<code>Employee<\/code>\u00a0class to initialize the\u00a0<code>name<\/code>,\u00a0<code>base_pay<\/code>, and\u00a0<code>bonus<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Delegating to other methods in the parent class<\/h2>\n\n\n\n<p>The&nbsp;<code>get_pay()<\/code>&nbsp;method of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class has some logic that is already defined in the&nbsp;<code>get_pay()<\/code>&nbsp;method of the&nbsp;<code>Employee<\/code>&nbsp;class. Therefore, you can reuse this logic in the&nbsp;<code>get_pay()<\/code>&nbsp;method of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class.<\/p>\n\n\n\n<p>To do that, you can call the\u00a0<code>get_pay()<\/code>\u00a0method of the\u00a0<code>Employee<\/code>\u00a0class in the\u00a0<code>get_pay()<\/code>\u00a0method of\u00a0<code>SalesEmployee<\/code>\u00a0class as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class SalesEmployee(Employee): def __init__(self, name, base_pay, bonus, sales_incentive): super().__init__(name, base_pay, bonus) self.sales_incentive = sales_incentive def get_pay(self): return super().get_pay() + self.sales_incentive<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following calls the\u00a0<code>get_pay()<\/code>\u00a0method of the\u00a0<code>Employee<\/code>\u00a0class from the\u00a0<code>get_pay()<\/code>\u00a0method in the\u00a0<code>SalesEmployee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>super().get_pay()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you call the&nbsp;<code>get_pay()<\/code>&nbsp;method from an instance of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class, it calls the&nbsp;<code>get_pay()<\/code>&nbsp;method from the parent class (<code>Employee<\/code>) and return the sum of the result of the&nbsp;<code>super().get_pay()<\/code>&nbsp;method with the&nbsp;<code>sales_incentive<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Put it all together<\/h2>\n\n\n\n<p>The following shows the complete code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee: def __init__(self, name, base_pay, bonus): self.name = name self.base_pay = base_pay self.bonus = bonus def get_pay(self): return self.base_pay + self.bonus class SalesEmployee(Employee): def __init__(self, name, base_pay, bonus, sales_incentive): super().__init__(name, base_pay, bonus) self.sales_incentive = sales_incentive def get_pay(self): return super().get_pay() + self.sales_incentive if __name__ == '__main__': sales_employee = SalesEmployee('John', 5000, 1000, 2000) print(sales_employee.get_pay()) <em># 8000<\/em><\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the Python&nbsp;super()&nbsp;to delegate to the parent class when overriding methods. Introduction to the Python super First, define an\u00a0Employee\u00a0class: The\u00a0Employee\u00a0class has three\u00a0instance variables\u00a0name,\u00a0base_pay, and\u00a0bonus. It also has the\u00a0get_pay()\u00a0method that returns the total of base_pay and bonus. Second, define the\u00a0SalesEmployee\u00a0class that inherits from the\u00a0Employee\u00a0class: The\u00a0SalesEmployee\u00a0class has four [&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-176","post","type-post","status-publish","format-standard","hentry","category-4-single-inheritance"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/176","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=176"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/176\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}