3 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
列出函數(shù)及其參數(shù):
ff=[ [function01, [10,20,"something"], dict(par1='hello', par2=847, par3=True, par4=17.821)],
[function02, [], dict(par43=(8.3+17.9i), par21='august')],
[function03, [1,2,"hello"], {}],
...
]
for i in range(17):
f1()
f2()
f3()
posargs=ff[i][1] # positional args
kw=ff[i][2] # keyword args for function_i
ff[i][0](*posargs,**kw) # calling function_i
r1()
r2()
r3()
您也可以稍后更改參數(shù)。例如:
ff[1][2]["par21"]="december"
new_kw_func02= dict(par43=(1+2i), par21='december'])
ff[1][2]= new_kw_func02
new_pargs_func03=[10,20,"World"]
ff[2][1]= new_pargs_func03

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
看看contextlib 模塊。非?;镜睦?/p>
from contextlib import ContextDecorator
def front1():
print('print from front1')
def front2():
print('print from front2')
def front3():
print('print from front3')
def rear1():
print('print from rear1')
def rear2():
print('print from rear2')
def rear3():
print('print from rear3')
class mycontext(ContextDecorator):
def __enter__(self):
front1()
front2()
front3()
return self
def __exit__(self, *exc):
rear1()
rear2()
rear3()
return False
@mycontext()
def foo():
print('print from foo')
@mycontext()
def bar():
print('print from bar')
if __name__ == '__main__':
foo()
bar()
# or as alternative
# my_funcs = [foo, bar]
# for func in my_funcs:
# func()
輸出
print from front1
print from front2
print from front3
print from foo
print from rear1
print from rear2
print from rear3
print from front1
print from front2
print from front3
print from bar
print from rear1
print from rear2
print from rear3
添加回答
舉報(bào)