python 中的 best/idioms 处理一系列功能检查的方法是什么

What is the best/idioms way in python to handle series of function check

请问python中的best/idioms方法是什么来处理这种代码逻辑的

list_a = []

def func_a():
  if some check not pass
     return False

  # check pass
  add some stuff to list_a and return True

def func_b():
  if some check not pass
     return False

  # check pass
  add some stuff to list_a and return True

def func_c():
  if some check not pass
     return False

  # check pass
  add some stuff to list_a and return True

def apply_function():
  if fun_a():
     return list_a
  if fun_b():
     return list_a
  if fun_c():
     return list_a
  ...

  return list_a   #empty list

如果有超过10个函数需要签入apply_function(),有没有更好的处理方式?

这可能对我有用

If funcA() or funcB() or funcC():
  return list_a

return list_a

这种情况下可以使用any()吗?

谢谢。

不要改变全局。如果检查失败,请将函数 return 列为一个列表或引发异常。这样你就可以 return 函数的结果,或者如果出现异常,继续下一个。

def func_a():
    if some check not pass
        raise ValueError('....')

    # check pass
    return [some, list]

# further functions that apply the same pattern.

def apply_function():
    for f in (func_a, func_b, func_c):
        try:
            return f()
        except ValueError:
            # check didn't pass, continue on to the next
            pass

异常是发出检查失败信号的理想方法,该函数告诉调用者它不能 return结果,因为条件这样做还没有被满足。如果没有发生异常,您可以相信 return 值是正确的。

请注意,函数只是对象,因此根据它们的名称,您可以将它们放在一个序列中并对其进行迭代。您也可以使用一些寄存器来添加更多功能来尝试全局列表。