I wanted to get a list of all the functions in a module and run them in the order they are written. This is not trivial to do because
inspect.getmembers will “return all the members of an object in a list of
(name, value) pairs sorted by name”. The sorting happens on the last line of that function, so I tried defining my own
getmembers with that line removed but that didn’t work because it uses
__dict__ which also don’t return properties in the order they are defined.
dicts are ordered now so it would make sense for those builtins to try and preserve a sensible order, which is obviously the order they were populated in.
Getting a list of module members in the order they are defined in was also asked about on Stack Overflow 10 years ago where the suggestion is to recover the order by looking at
.__code__.co_firstlineno but that doesn’t work for things that aren’t functions.