蝴蝶刀刀
使用列表列表和函数列表def f1(l1): # do whatever is required to the l1 listdef f2(l2): # similarly# and similarly for f3 .. f9...lofl = [ l1, l2, l3, l4, l5, l6, l7, l8, l9 ]loff = [ f1, f2, f3, f4, f5, f6, f7, f8, f9 ]for f, l in zip( loff, lofl): if l: # if the functions f cannot themselves safely do nothing for a falsy argument f( l) 希望所需的函数数量略小于九(在本例中)。您还可以轻松地向函数传递一个参数,因此函数可以是通用的,并告诉它在调用时要执行的变体操作for f, l, p in zip( loff, lofl, lofp): # or, zip( loff, lofl, list(range(9)) ) f(l, p)或者实际上,甚至将任意一组关键字参数传递给函数lofargs=[ { 'foo':1, 'bar':'Monty' }, # kwargs for f1 { 'foo':2, 'bar':'Python'}, { 'foo':3 }, {}, # no kwargs at all for f4, ... ]for f, l, k in zip( loff, lofl, lofargs): f( l, **k )