def print_class_methods_by_order(class_object):
for attr in class_object.__dict__:
if callable(getattr(class_object, attr)):
print(attr)
class MyObject:
def c(self):
pass
def a(self):
pass
def b(self):
pass
output:
>>> print_class_methods_by_order(MyObject)
c
a
b
Also works with decorators:
def print_class_methods_by_order(class_object):
for attr in class_object.__dict__:
if callable(getattr(class_object, attr)):
print(attr)
def my_decorator(func):
def wrapper(*args, **kwargs):
print("my_decorator")
func(*args, **kwargs)
return wrapper
class MyObject:
@my_decorator
def c(self):
pass
@my_decorator
def a(self):
pass
@my_decorator
def b(self):
pass
output:
>>> print_class_methods_by_order(MyObject)
c
a
b
And also works with "class" decorator (not sure there is any difference compared to the previous case):
def print_class_methods_by_order(class_object):
for attr in class_object.__dict__:
if callable(getattr(class_object, attr)):
print(attr)
def decorate_all(decorator):
def decorate(cls):
for attr in cls.__dict__:
if callable(getattr(cls, attr)):
setattr(cls, attr, decorator(getattr(cls, attr), cls))
return cls
return decorate
def my_decorator(func, cls):
def wrapper(*args, **kwargs):
print("my_decorator")
fn = func(*args, **kwargs)
return fn
return wrapper
@decorate_all(my_decorator)
class MyObject:
def c(self):
pass
def a(self):
pass
def b(self):
pass
output:
>>> print_class_methods_by_order(MyObject)
c
a
b
o
's methods are defined in two different classes, what order should they come in? – Venusian