学了 1 年 Python ,今天看了段代码觉得白学了,求教一下大家
把代码精简了一下

我本以为输出结果会是个[[],[]],输出结果是个列表嵌套了两个列表,每个列表里又嵌套了 2 个列表…

猜测是个递归,debug 一下确实是这样的

有几个问题:
1 、这个为啥是个递归?
2 、这个递归为什么没有爆栈?
3 、修改了一下代码,为什么返回了(True, True)?

把代码精简了一下

我本以为输出结果会是个[[],[]],输出结果是个列表嵌套了两个列表,每个列表里又嵌套了 2 个列表…

猜测是个递归,debug 一下确实是这样的

有几个问题:
1 、这个为啥是个递归?
2 、这个递归为什么没有爆栈?
3 、修改了一下代码,为什么返回了(True, True)?

大家忽略最后一个问题,脑残了
给你化简一下吧
root = []
root.append(root)
另外基本上所有语言都可以这么玩,不只是 Python
2.
爆栈对应于堆栈. 变量引用不会建立堆栈, 自然没有爆栈问题.
如果你准备用递归的函数来深层处理这个变量, 每次函数调用都会建立堆栈, 那基本就要爆了.
@SystemLight 一个修改 list 内容,一个修改 locals() 绑定。
# Use the old root to store the new key and result.
oldroot = root
oldroot[KEY] = key
oldroot[RESULT] = result
# Empty the oldest link and make it the new root.
# Keep a reference to the old key and old result to
# prevent their ref counts from going to zero during the
# update. That will prevent potentially arbitrary object
# clean-up code (i.e. __del__) from running while we’re
# still adjusting the links.
root = oldroot[NEXT]
oldkey = root[KEY]
oldresult = root[RESULT]
root[KEY] = root[RESULT] = None