python中methodcaller()函数执行类中所有的方法

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()分别调用,适合类方法较多时需要执行多有方法时使用。

 

posted on 2020-10-28 14:05  小白在此  阅读(1924)  评论(0编辑  收藏  举报

导航