我有这个python条件
a=a_foo()
if a==True:
b=b_foo():
if b==True:
c=c_foo()
if c==True:
d=d_foo()
else:
print('c error')
else:
print('b error')
else:
print('a error')
代码正在工作,但是如果我将此条件用于az,则代码似乎不好。
有什么更好的解决方案?
编辑:
感谢您的评论,我将if a
代替if a == True
还a_foo, b_foo, c_foo
函数仅仅是示例,可以是function1,func2,funcother,etc
最后,函数也有参数,例如 a_foo(p1, p2)
您只需要列出要调用的功能。但是,您将需要某种方法来识别哪个功能失败:
funcs = [a_foo,b_foo,c_foo,d_foo] # add more functions as required
for f in funcs:
if not f():
print(f.__name__, "error")
break
如果每个函数都希望有不同的参数集:
funcs = [(a_foo,),(b_foo,b),(c_foo,c,d),(d_foo,d,e,f)] # add more functions as required
for f,*args in funcs:
if not f(*args):
print(f.__name__, "error")
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句