from operator import methodcaller

class Cases:

  def methodA():

    pass

  def methodB():

    pass 

def main():
    case = Cases()
  for func in dir(Cases):
   if not func.startswith("__"):
     methodcaller(func)(case)
main()

会先后执行methodA()和methodB(),不用case.methodA();case.methodB()分别调用,适合类方法较多时需要执行多有方法时使用。

 

相关文章: