简体   繁体   English

Python:函数总是返回无

[英]Python: Function always returns None

I have some Python code that basically looks like this:我有一些 Python 代码,基本上如下所示:

my_start_list = ...

def process ( my_list ):
    #do some stuff

    if len(my_list) > 1:
        process(my_list)
    else:
        print(my_list)
        return my_list

print(process(my_start_list))

The strange thing is: print(my_list) prints out the correct content.奇怪的是: print(my_list)打印出正确的内容。 However, the second print statement printing the return value of the function always prints "None".但是,打印函数返回值的第二个打印语句总是打印“无”。 Even if I replace the normal return statement with return("abc") it is still None.即使我用return("abc")替换了正常的 return 语句,它仍然是 None。

As the content of the variable seems to be correct one line before the return statement, I don't know where to start debugging.由于变量的内容似乎在 return 语句前一行是正确的,我不知道从哪里开始调试。 Are there any common problems that may cause this?是否存在任何可能导致此问题的常见问题?

Here's what happening:这是发生的事情:

  1. You call process(my_start_list) .你打电话给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.在函数中,如果len(my_list) > 1则执行if块,并且那里没有 return 语句。 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 .现在,由于else尚未执行,并且因为这是您拥有 return 子句的唯一地方,所以您返回默认值None
  3. If you have 0 or 1 elements in your list, you return that list.如果列表中有 0 或 1 个元素,则返回该列表。

To fix this, you'd want to return the list returned by process(my_list) .要解决此问题,您需要返回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

You're only returning the list when you have 1 or 0 elements in it (the base case).只有当列表中有 1 或 0 个元素时(基本情况),您才返回列表。 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.您还需要在第一个块中使用 return 语句,在那里进行递归调用,或者深入到基本情况,将长度为 1 的列表返回到下一个级别,然后在剩下的过程中返回None . 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.现在每个案例(不仅仅是基本案例)都有一个返回值,因此该返回值将一直传播到您的初始调用。

You call process recursively but never ignore it's return value when you do.您递归调用process但在调用时绝不会忽略它的返回值。 Add a return statement to pass on the return value::添加一个return语句来传递返回值:

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.现在,当len(my_list) > 1True ,您实际上传递了递归调用的返回值。

As others have pointed out, you are missing a return statement.正如其他人指出的那样,您缺少return语句。

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 .我认为这使意图更加清晰,并且还避免了与尾递归相关的一些陷阱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM