Home > Net >  Class inheriting from itself in python
Class inheriting from itself in python

Time:01-17

I am fairly new to object oriented programming and I am learning neural networks with pytorch.

I have seen some examples where the class in python will inherit from itself (sub and super class will have the same name). Can someone please explain to me how does it work? I have put a small code as an example below.

import torch.nn as nn 


class LR(nn.module):
  def __init__(self, in_size, out_size):
    super(LR, self).__init__()
    self.linear = nn.Linear(in_size, out_size)

  def forward(self, x):
    out = self.linear(x)
    return out

I am auditing a pytorch course on coursera. The example is from the course. Here I am confused with the line super(LR, self)

CodePudding user response:

You might be mistaken in your assumption that you're seeing classes inherit from themselves. In the example you've posted LR inherits from nn.module, and super(LR, self).__init__() will be calling nn.module's __init__ method.

CodePudding user response:

@101 is right, This was my mistake. There are two conventions for inheritance in python. This is not a valid question itself.

@101 thank you for pointing out my mistake.

  • Related