Please note that this parsing has already been done for you when checking - look at inspect.findsource , which looks for a module to determine the class and returns the source and line number. Sorting by this line number (it may also be necessary to separate classes defined in separate modules) should give the correct order.
However, this function does not seem to be documented, and just uses a regular expression to find a string, so it may not be very reliable.
Another option is to use metaclasses or some other way to either implicitly or explicitly order the information for the object. For instance:
import itertools, operator next_id = itertools.count().next class OrderedMeta(type): def __init__(cls, name, bases, dct): super(OrderedMeta, cls).__init__(name, bases, dct) cls._order = next_id()
However, this is a rather intrusive change (you need to install a metaclass of any classes that interest you in OrderedMeta)
Brian source share