6

I understand the use of super() to inherit attributes and methods from a class when initializing your class. However, when using this in the context of neural networks such as:

class Net(nn.Module):
      def __init__(self, num_inputs, num_outputs, num_hiddens1, num_hiddens2,
                 is_training = True):
          super(Net, self).__init__()

I am confused on why super() is calling the same class. What does this mean and why is this done?

Thanks very much!

Merry
  • 215
  • 2
  • 7
  • 3
    You could also just do `super().__init__()`. This will call the `__init__` method of the superclass. – Jensun Ravichandran Dec 06 '20 at 22:05
  • 8
    The `super` doesn't "inherit attributes and methods", that is what `class Net(nn.Module)` does. Inherits from the parent. `super(Net, self).__init__()` calls the constructor of the parent so that any initialization done in the super class is still done. Otherwise your new class constructor would be the only constructor to run, which might not be enough or desirable. I.e. the superclass constructor might be doing something useful. – dpwr Dec 06 '20 at 22:08
  • 2
    See https://stackoverflow.com/a/64747570/13782669 – alex_noname Dec 07 '20 at 09:38
  • 1
    Thanks everyone! I think I get it now. – Merry Dec 09 '20 at 19:31

0 Answers0