{"id":2645,"date":"2021-10-25T02:17:44","date_gmt":"2021-10-25T02:17:44","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2645"},"modified":"2025-03-31T10:12:51","modified_gmt":"2025-03-31T10:12:51","slug":"python-super","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-super\/","title":{"rendered":"Python super"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Python <code>super()<\/code> to delegate to the parent class when overriding methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-super'>Introduction to the Python super <a href=\"#introduction-to-the-python-super\" class=\"anchor\" id=\"introduction-to-the-python-super\" title=\"Anchor for Introduction to the Python super\">#<\/a><\/h2>\n\n\n\n<p>First, define an <code>Employee<\/code> <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Employee<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus)<\/span>:<\/span>\n        self.name = name\n        self.base_pay = base_pay\n        self.bonus = bonus\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.base_pay + self.bonus<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Employee<\/code> class has three <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">instance variables<\/a> <code>name<\/code>, <code>base_pay<\/code>, and <code>bonus<\/code>. It also has the <code>get_pay()<\/code> method that returns the total of <code>base_pay<\/code> and <code>bonus<\/code>.<\/p>\n\n\n\n<p>Second, define the <code>SalesEmployee<\/code> class that inherits from the <code>Employee<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SalesEmployee<\/span><span class=\"hljs-params\">(Employee)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus, sales_incentive)<\/span>:<\/span>\n        self.name = name\n        self.base_pay = base_pay\n        self.bonus = bonus\n        self.sales_incentive = sales_incentive\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.base_pay + self.bonus + self.sales_incentive<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>SalesEmployee<\/code> class has four instance variables <code>name<\/code>, <code>base_pay<\/code>, <code>bonus<\/code>, and <code>sales_incentive<\/code>. It has the <code>get_pay()<\/code> method that <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-overriding-method\/\">overrides<\/a> the <code>get_pay()<\/code> method in the <code>Employee<\/code> class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='super-__init__'>super().__init__() <a href=\"#super-__init__\" class=\"anchor\" id=\"super-__init__\" title=\"Anchor for super().__init__()\">#<\/a><\/h3>\n\n\n\n<p>The <code>__init__()<\/code> method of the <code>SalesEmployee<\/code> class has some parts that are the same as the ones in the <code>__init__()<\/code> method of the <code>Employee<\/code> class.<\/p>\n\n\n\n<p>To avoid duplication, you can call the <code>__init__()<\/code> method of <code>Employee<\/code> class from the <code>__init__()<\/code> method of the <code>SalesEmployee<\/code> class.<\/p>\n\n\n\n<p>To reference the <code>Employee<\/code> class in the <code>SalesEmployee<\/code> class, you use the <code>super()<\/code>. The <code>super()<\/code> returns a reference of the parent class from a child class. <\/p>\n\n\n\n<p>The following redefines the <code>SalesEmployee<\/code> class that uses the <code>super()<\/code> to call the <code>__init__()<\/code> method of the <code>Employee<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SalesEmployee<\/span><span class=\"hljs-params\">(Employee)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus, sales_incentive)<\/span>:<\/span>\n        super().__init__(name, base_pay, bonus)\n        self.sales_incentive = sales_incentive\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.base_pay + self.bonus + self.sales_incentive<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you create an instance of the <code>SalesEmployee<\/code> class, Python will execute the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__init__\/\">__init__()<\/a><\/code> method in the <code>SalesEmployee<\/code> class. In turn, this <code>__init__()<\/code> method calls the <code>__init__()<\/code> method of the <code>Employee<\/code> class to initialize the <code>name<\/code>, <code>base_pay<\/code>, and <code>bonus<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='delegating-to-other-methods-in-the-parent-class'>Delegating to other methods in the parent class <a href=\"#delegating-to-other-methods-in-the-parent-class\" class=\"anchor\" id=\"delegating-to-other-methods-in-the-parent-class\" title=\"Anchor for Delegating to other methods in the parent class\">#<\/a><\/h2>\n\n\n\n<p>The <code>get_pay()<\/code> method of the <code>SalesEmployee<\/code> class has some logic that is already defined in the <code>get_pay()<\/code> method of the <code>Employee<\/code> class. Therefore, you can reuse this logic in the <code>get_pay()<\/code> method of the <code>SalesEmployee<\/code> class.<\/p>\n\n\n\n<p>To do that, you can call the <code>get_pay()<\/code> method of the <code>Employee<\/code> class in the <code>get_pay()<\/code> method of <code>SalesEmployee<\/code> class as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SalesEmployee<\/span><span class=\"hljs-params\">(Employee)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus, sales_incentive)<\/span>:<\/span>\n        super().__init__(name, base_pay, bonus)\n        self.sales_incentive = sales_incentive\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> super().get_pay() + self.sales_incentive<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following calls the <code>get_pay()<\/code> method of the <code>Employee<\/code> class from the <code>get_pay()<\/code> method in the <code>SalesEmployee<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">super().get_pay()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you call the <code>get_pay()<\/code> method from an instance of the <code>SalesEmployee<\/code> class, it calls the <code>get_pay()<\/code> method from the parent class (<code>Employee<\/code>) and return the sum of the result of the <code>super().get_pay()<\/code> method with the <code>sales_incentive<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='put-it-all-together'>Put it all together <a href=\"#put-it-all-together\" class=\"anchor\" id=\"put-it-all-together\" title=\"Anchor for Put it all together\">#<\/a><\/h2>\n\n\n\n<p>The following shows the complete code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Employee<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus)<\/span>:<\/span>\n        self.name = name\n        self.base_pay = base_pay\n        self.bonus = bonus\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.base_pay + self.bonus\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SalesEmployee<\/span><span class=\"hljs-params\">(Employee)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, base_pay, bonus, sales_incentive)<\/span>:<\/span>\n        super().__init__(name, base_pay, bonus)\n        self.sales_incentive = sales_incentive\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_pay<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> super().get_pay() + self.sales_incentive\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    sales_employee = SalesEmployee(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">5000<\/span>, <span class=\"hljs-number\">1000<\/span>, <span class=\"hljs-number\">2000<\/span>)\n    print(sales_employee.get_pay())  <span class=\"hljs-comment\"># 8000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgRW1wbG95ZWU6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSwgYmFzZV9wYXksIGJvbnVzKToKICAgICAgICBzZWxmLm5hbWUgPSBuYW1lCiAgICAgICAgc2VsZi5iYXNlX3BheSA9IGJhc2VfcGF5CiAgICAgICAgc2VsZi5ib251cyA9IGJvbnVzCgogICAgZGVmIGdldF9wYXkoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuYmFzZV9wYXkgKyBzZWxmLmJvbnVzCgoKY2xhc3MgU2FsZXNFbXBsb3llZShFbXBsb3llZSk6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSwgYmFzZV9wYXksIGJvbnVzLCBzYWxlc19pbmNlbnRpdmUpOgogICAgICAgIHN1cGVyKCkuX19pbml0X18obmFtZSwgYmFzZV9wYXksIGJvbnVzKQogICAgICAgIHNlbGYuc2FsZXNfaW5jZW50aXZlID0gc2FsZXNfaW5jZW50aXZlCgogICAgZGVmIGdldF9wYXkoc2VsZik6CiAgICAgICAgcmV0dXJuIHN1cGVyKCkuZ2V0X3BheSgpICsgc2VsZi5zYWxlc19pbmNlbnRpdmUKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgc2FsZXNfZW1wbG95ZWUgPSBTYWxlc0VtcGxveWVlKCdKb2huJywgNTAwMCwgMTAwMCwgMjAwMCkKICAgIHByaW50KHNhbGVzX2VtcGxveWVlLmdldF9wYXkoKSkgICMgODAwMA%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">8000<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>super()<\/code> to call the methods of a parent class from a child class.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=super\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2645\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-super\/\"\n\t\t\t\tdata-post-title=\"Python super\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2645\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-super\/\"\n\t\t\t\tdata-post-title=\"Python super\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Python super() to delegate to the parent class when overriding methods.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2645","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2645"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2645\/revisions"}],"predecessor-version":[{"id":7315,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2645\/revisions\/7315"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}