简体   繁体   English

Python 递归返回 列表中没有

[英]Python recursion return None on list

Hi I am trying to practice on recursion question and have been facing an issue嗨,我正在尝试练习递归问题并且一直面临问题

def recurr(container = [], index = 0):
    print(str(container) + " " + str(index))
    if index == 10:
        return recurr

    recurr(container.append(index), index+1)

print(recurr())

This function takes container as list and index as int, now I am trying to append index in container and increment index.这个 function 将容器作为列表和索引作为 int,现在我正在尝试在容器中创建 append 索引并增加索引。 But whenever the function is called the value of the container becomes 'None' and I get an error "AttributeError: 'NoneType' object has no attribute 'append'".但是,每当调用 function 时,容器的值就会变为“无”,并且出现错误“AttributeError:'NoneType' object 没有属性'附加'”。 I am not sure what am I doing wrong so if someone can help me understand that I would be really grateful.我不确定我做错了什么,所以如果有人能帮助我理解我将非常感激。

Thank you谢谢

What Tom Karzes mentioned above is correct.上面提到的 Tom Karzes 是正确的。 There are a few issues有几个问题

  1. container.append(index) does not return the container itself. container.append(index)不返回容器本身。 The .append() method appends an item to a container, but does it in place. .append()方法将一个项目附加到一个容器中,但它是在原地完成的。 You need to pull that command up a line, and then pass the container to recurr() .您需要将该命令拉上一行,然后将容器传递给recurr()

  2. The issue with the default argument being an empty list is also a valid point but probably beyond the scope of this discussion.默认参数为空列表的问题也是一个有效点,但可能超出了本讨论的 scope。 Just pass in a list explicitly只需明确传入一个列表

  3. Your return statement is returning the function when it should probably be returning the container.您的 return 语句正在返回 function,而它可能应该返回容器。

  4. The last line needs to return function call return recurr(container, index+1) .最后一行需要返回 function call return recurr(container, index+1) Without it, you will get all the print statements but the final return value will STILL be None.没有它,您将获得所有打印语句,但最终返回值仍为 None。 To understand why, you need to understand a bit about stack frames and function call frames.要了解原因,您需要了解一点关于堆栈帧和 function 调用帧的知识。 Each function is calling itself, and when it does, a new stack frame is added, the calling frame can only exit when all downstream frames exit.每个 function 都在调用自己,调用时会添加一个新的堆栈帧,只有当所有下游帧都退出时,调用帧才能退出。 When the final frame exits, it returns the full container, but it returns it to its caller, which is recurr(container, 9) .当最后一帧退出时,它返回完整的容器,但它会将它返回给它的调用者,即recurr(container, 9) Of course, if you look at the function, there are NO other return statements, so that frame returns None, and each subsequent frame continues to return None.当然,如果你看function,没有其他的return语句,所以frame返回None,后续的每一帧都继续返回None。

Try this:尝试这个:

def recurr(container, index = 0):
    print(str(container) + " " + str(index))
    if index == 10:
        return container
    
    container.append(index)
    return recurr(container, index+1)

x = []
print(recurr(x))

This can be tricky to fully understand so please ask questions and do a little experimentation这可能很难完全理解,所以请提出问题并做一些实验

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

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