{"id":5078,"date":"2020-04-24T15:40:11","date_gmt":"2020-04-24T15:40:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5078"},"modified":"2022-08-06T13:28:37","modified_gmt":"2022-08-06T13:28:37","slug":"super-method-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/super-method-in-python","title":{"rendered":"Understanding the super() Method in Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>Today in this tutorial, we are going to discuss <strong>the super() method in Python<\/strong>. <\/p>\n\n\n\n<p>Before diving right into the topic, we highly recommend going through the tutorial on <a aria-label=\" (opens in a new tab)\" class=\"rank-math-link rank-math-link\" rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\/oops\/inheritance-in-python\" target=\"_blank\">Python Inheritance<\/a>.<\/p>\n\n\n\n<p>Once you know about inheritance in Python, we can get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The super() Method in Python<\/h2>\n\n\n\n<p>The <code>super<\/code> method returns a proxy object that delegates method calls to a parent or sibling class of <strong>type<\/strong>. This is useful for accessing inherited methods that have been overridden in a class. <\/p>\n\n\n\n<p>Or simply, it is used to call the constructor, i.e. the<code> __init__()<\/code> method of the <strong>superclass<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax<\/strong> for using super in Python is given below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsuper(&#x5B;type&#x5B;, object-or-type]])\n<\/pre><\/div>\n\n\n<p>In <strong>Python 3.x<\/strong> versions, we can use super without passing the above two parameters. Look at the code snippet below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass C(B):\n    def method(self, arg):\n        super().method(arg)\n<\/pre><\/div>\n\n\n<p>Here, <code>C<\/code> is the derived class, <code>B<\/code> is the base class, method is a user defined function with argument <code>arg<\/code>. <\/p>\n\n\n\n<p>As you can see, the line <code>super().method(arg)<\/code> is actually equivalent to <code>super( C, self).method(arg)<\/code> in Python <strong>3.x<\/strong>. This is not allowed in Python <strong>2.x<\/strong>. Hence, using super there is a bit tricky.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using super()<\/h2>\n\n\n\n<p>Consider the below given example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Demo:\n    a = 0\n    b = 0\n    c = 0\n    def __init__(self,A,B,C):\n        self.a = A\n        self.b = B\n        self.c = C\n    def display(self):\n        print(self.a, self.b, self.c)\n\nclass Newdemo(Demo):\n    d = 0\n    def __init__(self,A,B,C,D):\n        self.a = A\n        self.b = B\n        self.c = C\n        self.d = D\n\n    def display(self):\n        print(self.a, self.b, self.c,self.d)\n\nB1 = Demo(100,200,300)\nprint(&quot;Contents of Base Class:&quot;)\nB1.display()\nD1 = Newdemo(10,20,30,40)\nprint(&quot;Contents of Derived Class:&quot;)\nD1.display()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"261\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output1.png\" alt=\"Output1\" class=\"wp-image-5079\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output1.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output1-300x174.png 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Output<\/figcaption><\/figure><\/div>\n\n\n\n<p>In the above example, the classes derived from the base class <code>Demo<\/code> were not implemented efficiently or robustly.<\/p>\n\n\n\n<p>The derived class <code>Newdemo<\/code> explicitly initializes the value of A, B, and C fields of the base class. The same duplication of code is found while initialization of the same fields in the base class, <code>Demo<\/code> too. <\/p>\n\n\n\n<p>This process is inefficient. This implies that a <strong>subclass<\/strong> must be granted access to the members of a <strong>superclass<\/strong>.<\/p>\n\n\n\n<p>Therefore, whenever a subclass needs to refer to its immediate super class, <code>super<\/code> comes into the picture.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Super() to Call Super Class Constructor<\/h3>\n\n\n\n<p>Now let us apply the <code>super()<\/code> method for the above example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Demo:\n    a = 0\n    b = 0\n    c = 0\n    def __init__(self,A,B,C):\n        self.a = A\n        self.b = B\n        self.c = C\n    def display(self):\n        print(self.a, self.b, self.c)\n\nclass Newdemo(Demo):\n    d = 0\n    def __init__(self,A,B,C,D):\n        self.d = D\n        super().__init__(A,B,C) #super to call super Class\n        #The __init__() Method\n\n    def display(self):\n        print(self.a, self.b, self.c,self.d)\n\nB1 = Demo(12,34,56)\nprint(&quot;Contents of Base Class:&quot;)\nB1.display()\nD1 = Newdemo(11,22,33,44)\nprint(&quot;Contents of Derived Class:&quot;)\nD1.display()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"282\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output.png\" alt=\"Output\" class=\"wp-image-5080\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/output-300x188.png 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Output<\/figcaption><\/figure><\/div>\n\n\n\n<p>Here, the derived class <code>Newdemo<\/code> calls the <code>super()<\/code> with arguments <strong>a<\/strong>, <strong>b<\/strong>, and <strong>c<\/strong>. This causes the constructor <code>__init__<\/code> of the base class, i.e. <code>Demo<\/code> to be called. This initialises the values of <strong>a<\/strong>, <strong>b<\/strong>, and <strong>c<\/strong>. Hence, the <code>Newdemo<\/code> class no longer initialises the values itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using super in Python 2.x<\/h2>\n\n\n\n<p>The <strong>syntax<\/strong> to call a <code>super<\/code> class constructor in <strong>Python 2.x<\/strong> is given below,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsuper(Derived_Class_Name, self).__init__(Parameters_of_Super_Class_Constructor)\n<\/pre><\/div>\n\n\n<p>Hence we need to make some minor changes to the above example if we want to use it in Python <strong>2<\/strong>.<\/p>\n\n\n\n<p>Firstly, we need to put <code>object<\/code> in the base class as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Demo(object):\n...#other statements\n<\/pre><\/div>\n\n\n<p>And secondly, pass <code>Newdemo<\/code> and <code>self<\/code> at the site of super class call. Like this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsuper(Newdemo,self).__init__(A,B,C) #super to call super Class\n...        #The __init__() Method\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Why we need super()<\/h2>\n\n\n\n<p>In the case of <strong>single inheritance<\/strong> with a parent and offspring class, the <code>super<\/code> function is used to implicitly refer to the parent class without naming it explicitly. This makes the code more efficient, maintainable and robust in nature.<\/p>\n\n\n\n<p>Nextly, for a <strong>multi-level inheritance<\/strong>, the <code>super<\/code> method can be used to refer to the immediate superclass implicitly. This again makes the code easier to understand and highly maintainable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So in this tutorial we understood the concept of <strong>super() method in Python<\/strong>, its use and need.<\/p>\n\n\n\n<p>For any further questions, feel free to comment below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Super &#8211; Journal Dev Post,<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/222877\/what-does-super-do-in-python-difference-between-super-init-and-expl\" target=\"_blank\" label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">What does &#8216;super&#8217; do in Python?<\/a> &#8211;  StackOverflow Question,<\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#super\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Python super<\/a> &#8211; Official Documentation.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Today in this tutorial, we are going to discuss the super() method in Python. Before diving right into the topic, we highly recommend going through the tutorial on Python Inheritance. Once you know about inheritance in Python, we can get started. The super() Method in Python The super method returns a proxy object that [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":5084,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5078"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5078\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5084"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}