programmer_04_03
programmer_04_03

Reputation: 181

Class inheriting from itself in python

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)

Upvotes: 0

Views: 146

Answers (1)

101
101

Reputation: 8999

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.

Upvotes: 1

Related Questions