def test(x,theList):
theList.append(x)
if x < 2:
x = x + 1
test(x,theList)
print x
print theList
test(1,[])
为什么结果[1,2]?不仅仅是[1]?
答案 0 :(得分:2)
因为在递归调用print
之后执行test()
语句。
Python对象总是通过引用传递,所以当第二次调用测试调用theList.append(x)
时,它会附加到传入的原始列表中,然后打印出来。
答案 1 :(得分:0)
def test(x,theList):
if x < 2:
theList.append(x)
x = x + 1
test(x,theList)
print x
print theList
test(1,[])