it outputs submodules in the order of addition in the class A, i.e., c, b, a, instead of alphabetical order a, b, c I would expect. What python technique is used here to achieve this functionality? How does the base class (nn.Module) inspect live the derived classes’ (A) submodules addition? Thanks.
Children are kept in an collections.OderedDict, a Python dictionary to preserve order.
The nn.Module class does some clever magic around __setattr__ and friends to keep the submodules (and parameters and buffers) in separate dictionaries.
You can look at the definition, it is clever yet reasonably straightforward.
Well as code is executed in a linear way (downwards…), and self.something = ... calls __setattr__, it is called in the correct order (c, b, a), and the OrderedDict will keep the order by definition. Is that clearer?