class OrderedMeta(type):

    def __init__(cls, name, bases, dct):
        super(OrderedMeta, cls).__init__(name, bases, dct)
cls._order = next_id()
__metaclass__ = OrderedMeta


class A:


    class D:
        pass


class C:
    pass
print(sorted([cls for cls in [getattr(A, name) for name in dir(A)] if
    isinstance(cls, OrderedMeta)], key=operator.attrgetter('_order')))
Python introspection: How to get an 'unsorted' list of object attributes?