krishma_thakral
krishma_thakral

Reputation: 23

Return values from multiple if condition from a same function in python

I want to return values from multiple if condition from a same function in python and afterwards want to use all the return values from each if condition. Is it possible????

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)

I want both result_failure and test_failure values. Is there a way except creating separate functions for each of them?

Upvotes: 1

Views: 8427

Answers (1)

Samuel Wölfl
Samuel Wölfl

Reputation: 33

I don't exactly know what you want to achieve, but probably just returning multiple values will help you:

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)

Like this you can return multiple variables with one function. Just separate them with a comma. They will be returned as a tuple. To fetch them separate you can also declare 2 variables separated with a comma with your function.

But in your example result_failure and test_failure are getting the same value. And your if-statements seem redundand, because you just trigger them with the string above that you set yourself. So if this wasn't helpful it would be interesting what your goals are with this script.

Upvotes: 2

Related Questions