How does module.children() know how to sort submodules?

When I run

class A(nn.Module):                  
    def __init__(self):              
        self.c = nn.Conv2d(55, 50, 3)
        self.b = nn.Conv2d(50, 50, 3)
        self.a = nn.Conv2d(25, 25, 3)

 a = A()

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.

Best regards


But how does the base class inspect the order of member modules in the derived class? I updated the question.

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?

Ok, thanks! nn.Module basically overrides the __setattr__ method to achieve this.