How does a Module know about it's component modules?

The documentation says "Submodules assigned in this way will be registered, " but does not actually say how this magical automatic registration happens. Could anyone explain?

Another and maybe related question for me is what kind of magic
super(Model, self).__init__()
is doing?

These are basically python tricks. In Python, when you assign something as an attribute of an object, the object’s __setattr__ method will be called. For nn.Module, the method is overriden at

super(Model, self).__init__() is not magic either. It’s the standard way to call parent class’s constructor in Python.


Thank you – so the only way of how the submodules get registered is when an attribute is directly assigned an instance of a Module subclass.
In all other cases, e.g. assigning to a component or element of an attribute or assigning something that contains Module subclass instance, this does not work and one has to call self.add_module(...) for the submodule?

Yes! That’s correct :slight_smile:

1 Like