2016-03-28 31 views

回答

0

这是因为方式列表工作在Python中,它不会发送函数列表。它将函数发送到已存在列表所在的内存中,然后可以更改它

1

这是因为列表在Python中是可变的,并且您的函数修改了lst。事实上,它与缺失的return声明无关 - 所有这一切意味着如果您有x = f(lst),x将是None。如果您想在lst上执行f而不改变它,请发送副本。这里有一个例子:

lst = [1, 2, 3] 

def fn(lst): 
    print("in fn") 
    lst[1] = 10 

x = lst[::] # make a copy 
print("X before is:", x) 
fn(x) 
print("X after is:", x) 
print("Lst after calling fn with x but before using Lst is:", lst) 
fn(lst) 
print("Lst after is:", lst) 

此打印:

X before is: [1, 2, 3] 
in fn 
X after is: [1, 10, 3] 
Lst after calling fn with x but before using Lst is: [1, 2, 3] 
in fn 
Lst after is: [1, 10, 3] 
相关问题