Super().__init__() vs super(classname,self).__init__()

sandeep1: <p>I have the doubt regarding what exactly does the super do?<br>
In the below example for class BertPooler the init of base class is called without any parameter to super , While in the class TypeClassifier the super is initialised by super(TypeClassifier, self)</p>
<p>How does both are different and what is the role of it?</p>
<p>Thank you.</p>
<pre><code class="lang-auto">
class BertPooler(torch.nn.Module):
    def __init__(self, config):
        super().__init__() 
        self.dense = torch.nn.Linear(config.hidden_size, config.hidden_size)
        self.activation = torch.nn.Tanh()

    def forward(self, hidden_states):
        # We "pool" the model by simply taking the hidden state corresponding
        # to the first token.
        first_token_tensor = hidden_states[:, 0]
        pooled_output = self.dense(first_token_tensor)
        pooled_output = self.activation(pooled_output)
        return pooled_output


class TypeClassifier(torch.nn.Module):
    def __init__(self, model, n_labels):
        """
        In the constructor we instantiate two nn.Linear modules and assign them as
        member variables.
        """
        super(TypeClassifier, self).__init__()
        self.bert_model = model

</code></pre>

Matias_Vasquez: <p>Hi,</p>
<aside class="quote no-group" data-username="sandeep1" data-post="1" data-topic="148793">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandeep1/48/12454_2.png" class="avatar"> sandeep1:</div>
<blockquote>
<p>what is the role of it?</p>
</blockquote>
</aside>
<p>when you define your classes like this:</p>
<pre data-code-wrap="python"><code class="lang-python">class MyClass(ClassToInheritFrom):
    ...
</code></pre>
<p>You are inheriting from the <code>Base</code> class. In my example would be the <code>ClassToInheritFrom</code> class. In your examples, both classes inherit from <code>torch.nn.Module</code>.</p>
<p>Now this <code>torch.nn.Module</code> has its own methods (like <code>__init__()</code>). So if you do <strong>NOT</strong> define these methods in your own class, they are still available, because the parent class has them, but they do only the stuff the base class has defined.</p>
<p>However, if you <strong>DO</strong> write your own <code>__init__()</code> method inside your class, the one from the base gets overwritten. You can then use <code>super()</code> to access the base class and then call the method so that both the stuff from the <code>base</code> as well as your stuff gets done.</p>
<aside class="quote no-group" data-username="sandeep1" data-post="1" data-topic="148793">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandeep1/48/12454_2.png" class="avatar"> sandeep1:</div>
<blockquote>
<p>How does both are different</p>
</blockquote>
</aside>
<p>In your case, they are the same. In previous versions of python you <strong>HAD TO</strong> to it like this ↓</p>
<aside class="quote no-group" data-username="sandeep1" data-post="1" data-topic="148793">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandeep1/48/12454_2.png" class="avatar"> sandeep1:</div>
<blockquote>
<p><code>super(TypeClassifier, self).__init__()</code></p>
</blockquote>
</aside>
<p>But in python 3 it changed and you can use ↓ to make it easier.</p>
<aside class="quote no-group" data-username="sandeep1" data-post="1" data-topic="148793">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandeep1/48/12454_2.png" class="avatar"> sandeep1:</div>
<blockquote>
<p><code>super().__init__() </code></p>
</blockquote>
</aside>
<p>You can also have multiple inheritance. Here is a little dumb example</p>
<pre data-code-wrap="python"><code class="lang-python">class A:
    def __init__(self):
        print("This is class A")

class B:
    def __init__(self):
        print("This is class B")

class C(A, B):
    def __init__(self):
        # This will look for the first base class that implements this method
        super().__init__()        

        # You can also explicitly call one of the base classes
        B.__init__(self)

        # This is what I wanted to do, extra to the base class
        print("This is class C")

c = C()
</code></pre>
<pre data-code-wrap="python"><code class="lang-python"># Output:
#This is class A
#This is class B
#This is class C
</code></pre>
<p>Maybe this can help you ↓ understand more about inheritance</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://realpython.com/python-super/#super-in-multiple-inheritance">
  <header class="source">
      <img src="https://realpython.com/static/favicon.68cbf4197b0c.png" class="site-icon" width="" height="">

      <a href="https://realpython.com/python-super/#super-in-multiple-inheritance" target="_blank" rel="noopener nofollow ugc">realpython.com</a>
  </header>

  <article class="onebox-body">
    <img width="" height="" src="https://files.realpython.com/media/The-Super-Function-in-Python_Watermarked.58312d5835f7.jpg" class="thumbnail">

<h3><a href="https://realpython.com/python-super/#super-in-multiple-inheritance" target="_blank" rel="noopener nofollow ugc">Supercharge Your Classes With Python super() – Real Python</a></h3>

  <p>In this step-by-step tutorial, you will learn how to leverage single and multiple inheritance in your object-oriented application to supercharge your classes with Python super().</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>

jyang: <p>Hi,</p>
<p>I heared someone said that because of something in back propagation we need to use the complex way. Is that true?</p>
<p>Here is where I found it: <a href="https://stackoverflow.com/questions/49634682/pytorch-superclass-self-init-vs-super-init" rel="noopener nofollow ugc">Pytorch super(class, self).<strong>init</strong>() vs super().<strong>init</strong>() - Stack Overflow</a></p>

umarzein: <p>In the post of the stackoverflow link it says “backwards compatibility”, meaning if you use older python versions, it can still run it, and has nothing to do with back propagation</p>

jyang: <p>Thanks a lot! It really makes me confused for a long time</p>