Python: Function always returns None

前端 未结 4 751
迷失自我
迷失自我 2020-11-29 11:36

I have some Python code that basically looks like this:

my_start_list = ...

def process ( my_list ):
    #do some stuff

    if len(my_list) > 1:
                


        
相关标签:
4条回答
  • 2020-11-29 12:02

    Here's what happening:

    1. You call process(my_start_list).
    2. In the function, the if block is executed if len(my_list) > 1, and there are no return statement there. Now, since the else has not been executed and since that is the only place where you have the return clause, you return the default which is None.
    3. If you have 0 or 1 elements in your list, you return that list.

    To fix this, you'd want to return the list returned by process(my_list).

    That is:

    def process(my_list):
        # do some stuff
        ...
        if len(my_list) > 1:
            return process(my_list)
        else:
            print(my_list)
            return my_list
    
    0 讨论(0)
  • 2020-11-29 12:04

    You're only returning the list when you have 1 or 0 elements in it (the base case). You need a return statement in the first block as well, where you make the recursive call, or else you drill down to the base case, return the length-1 list to the next level, and then return None the rest of the way up. So what you want looks like this, instead:

    def process(my_list):
        # Do some stuff.
        if len(my_list) > 1:
            return process(my_list) #If you don't return this result, you return None
        else:
            print(my_list)
            return my_list
    

    Now every case (not just the base case) has a return value, so that return value will propagate all the way back up to your initial call.

    0 讨论(0)
  • 2020-11-29 12:05

    You call process recursively but never ignore it's return value when you do. Add a return statement to pass on the return value::

    def process ( my_list ):
        #do some stuff
    
        if len(my_list) > 1:
            return process(my_list)
        else:
            print(my_list)
            return my_list
    

    Now, when len(my_list) > 1 is True, you actually pass on the return value of the recursive call.

    0 讨论(0)
  • 2020-11-29 12:13

    As others have pointed out, you are missing a return statement.

    I personally would turn that tail recursion into iteration:

    def process(my_list):
        while True:
            # do some stuff
            if len(my_list) <= 1:
                return my_list
    

    I think this makes the intent a little clearer, and also avoids some pitfalls associated with tail recursion.

    0 讨论(0)
提交回复
热议问题