No. Class members are not ordered. They are compiled into a dictionary, immediately losing order. You can resort to tricks, for example, disassemble the source, but it will easily break. For starters, the source may not be available.
[edit: it seems that python3 provides more flexibility in creating the class, allowing you to customize the way you collect class members , if you are only on python3, this is probably the best approach]
If changing the code is not a problem, you can use the decorator:
import inspect def track_order(fn): fn.order = track_order.idx track_order.idx += 1 return fn track_order.idx = 0 class Obj(object): @track_order def c(self): return 1 @track_order def b(self): return 2 @track_order def a(self): return 3 o = Obj() methods = sorted((item for item in inspect.getmembers(o, inspect.ismethod)), key=lambda item: item[1].order) for name, value in methods: print str(value())+" "+name
The decorator adds the idx attribute to all methods that pass through it. This exploits the fact that python has first-class features.
$ python test.py 1 c 2 b 3 a
Note. This is the method used by Django to track the order of forms and model fields. Only they do not need a decorator, because field classes have a built-in instanciation order attribute (called creation_counter ).
source share