I am studying the code of Fast Depth and I am wondering what is the meaning of writing the __init__
method of the class like this:
class MobileNet(nn.Module):
def __init__(self, decoder, output_size, in_channels=3, pretrained=True):
super(MobileNet, self).__init__()
or this (always the same)
class MobileNetSkipAdd(nn.Module):
def __init__(self, output_size, pretrained=True):
super(MobileNetSkipAdd, self).__init__()
I don't understand the usage of the super()
function, where it is called the class itself.
The super
function gets you the parent class, in your example nn.Module
. Note that since Python 3 you can just say:
super().__init__()
When used with __init__
, it lets you perform the initialization code defined in the parent, on the instance you're creating. It's usually followed by initialization code that is specific to this subclass.
Since the MobileNet
subclass is a kind of nn.Module
, a specialized case of the more general nn.Module
if you will, it makes sense to perform the general initialization first, and then the specific initialization.