2016-03-28 12 views

答えて

0

これは、リストがPythonで動作するため、リストの関数を送信しないためです。既に存在するリストがあるメモリ内の場所に関数を送り、それを変更することができます。

1

これはリストがPythonで変更可能で、関数がlstを変更するためです。実際には、これは紛失したreturnの声明とは関係がありません。つまり、x = f(lst)xの場合はNoneとなります。 lstfを差し替えて実行したい場合は、コピーを送信してください。ここでは例です:

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] 
関連する問題