也许您可以尝试这样的事情:
def call_attrs(obj, attrs_list, *args):
for attr in attrs_list:
if hasattr(obj, attr):
bound_method = getattr(obj, attr)
return bound_method(*args)
raise AttributeError
您可以这样称呼它:
call_attrs(a, ['method1', 'method2', 'method3'])
这将尝试按它们在列表中的顺序调用这些方法。如果您想传递任何参数,可以像下面这样在列表之后传递它们:
call_attrs(a, ['method1', 'method2', 'method3'], arg1, arg2)