Home > front end >  python3: A sub-funtion of a class cannot correctly return its value. And the final value returned fr
python3: A sub-funtion of a class cannot correctly return its value. And the final value returned fr

Time:04-10

The simplified codes:

class A:
    def __init__(self):
        self.a = True

    def test(self):
        # if self.a is True:
        #     return 1
        self.sub_test()
        return 0

    def sub_test(self):
        if self.a is True:
            return 1


if __name__ == '__main__':
    a = A()
    print(a.test())

when I debugged the program, found the process had stepped into return 1 but didn't correctly return the value 1, then jump into return 0. and the 0 is the final value. However, the code below could corrrectly return the value 1.

class A:
    def __init__(self):
        self.a = True

    def test(self):
        if self.a is True:
            return 1
        # self.sub_test()
        return 0

    def sub_test(self):
        if self.a is True:
            return 1


if __name__ == '__main__':
    a = A()
    print(a.test())

I looked up to some websites but did not get the answer. I think it maybe like the 'void' function of C so it rejects the return value, I don't know exactly. But in python, it has no error info and confuse me. Could somebody tell me why the mistake happended? Are there any measure to divide a long function of a class in python3 to several sub-funcs and make it works correctly?

CodePudding user response:

This is because 0 is being returned from the sub_test function, but not the test function.

Remember that when a function is called, it is not the same as copying and pasting its code into a new place; the return statement is only running within the sub-function.

What you might want to do is something like this:

class A:
    def __init__(self):
        self.a = True

    def test(self):
        if self.sub_test():
            return 1
        return 0

    def sub_test(self):
        if self.a is True:
            return 1


if __name__ == '__main__':
    a = A()
    print(a.test())

CodePudding user response:

It is not a mistake - but error in the code

What is the error, How to fix it :-

  • Error is that you are just getting a value from a subfunction. the subfunction sub_test() is returning the value to class function test() not actually to the main program
  • To fix it, change self.sub_test() to return self.sub_test()

fixed code :-

class A:
    def __init__(self):
        self.a = True
    
    def test(self):
        # here you have to return the value got from subfunction
        return self.sub_test()
    
    def sub_test(self):
        if self.a is True:
            return 1
        return 0 # to return 0 if not 1

CodePudding user response:

It is not a mistake, the sub_test function actually returns 1 to the test function but after it returns 1 you are returning 0 back to function call. That's why it prints 0 instead of 1.

If you want to receive the 1 sent by sub_test you have to write the test function in the following manner

def test(self):
   return self.sub_test()

CodePudding user response:

You need to use the value returned from sub_test. Here is a naive way to fix your test function:

def test(self):
    value = self.sub_test()
    if value == 1:
        return value
    return 0

You can then short-circuit it further:

def test(self):
    return self.sub_test() or 0

This last approach works because your sub_test either return 1 or None (implicitly), depending on the value of self.a. If it returns a 1, then the expression self.sub_test() or 0 becomes 1. If it returns a None, then the same expression becomes 0.

  • Related