我想从python中的同一函数中的多个if条件返回值,然后想使用每个if条件的所有返回值。有可能吗????
class Failure():
@staticmethod
def user_failure(input):
bad_char = ['(', ')']
failure_reason = re.findall("(FAIL)(.*)", input)[0][1]
failure_reason = ''.join(
i for i in failure_reason if not i in bad_char).strip()
return failure_reason
def get_failure_reason(self):
result = 'RESULT => FAIL(Failure includes TCs)'
test = 'Test => FAIL (Test log not found)'
if 'FAIL' in result:
result_failure = Failure.user_failure(result)
return result_failure
if 'FAIL' in test:
test_failure = Failure.user_failure(test)
return test_failure
obj = Failure()
output = obj.get_failure_reason()
print(output)
我想要result_failure和test_failure两个值。除了为它们中的每一个创建单独的函数之外,还有其他方法吗?
我不知道你想要实现什么,但可能只是返回多个值会对你有所帮助:
def get_failure_reason(self):
result = 'RESULT => FAIL(Failure includes TCs)'
test = 'Test => FAIL (Test log not found)'
if 'FAIL' in result:
result_failure = Failure.user_failure(result)
if 'FAIL' in test:
test_failure = Failure.user_failure(test)
return result_failure, test_failure
result_failure, test_failure = obj.get_failure_reason()
print(result_failure, test_failure)
像这样,您可以用一个函数返回多个变量。用逗号把它们隔开。它们将作为元组返回。要单独获取它们,您还可以在函数中声明用逗号分隔的2个变量。
但是在您的示例中,result_failure和test_failure得到了相同的值。你的if语句看起来是多余的,因为你只需要用上面你自己设置的字符串来触发它们。因此,如果这没有帮助,那么这个脚本的目标是什么就很有趣了。