我剛開始學習Python的我變得有點糊塗看到下面的程序後輸出:Python程序令人驚訝的輸出
#!/usr/bin/python
# Function definition is here
def changeme(mylist):
"This changes a passed list into this function"
mylist = [1,2,3,4]; # This would assig new reference in mylist
print "Values inside the function: ", mylist
return
# Now you can call changeme function
mylist = [10,20,30];
changeme(mylist);
print "Values outside the function: ", mylist
值在函數內部:在函數外[1, 2, 3, 4]
值:[10, 20, 30]
爲什麼值超出函數:[10, 20, 30]
,而不是[1, 2, 3, 4]
,因爲我們通過引用將參數傳遞給函數?
沒有看到。再試一次。 – 2011-05-31 07:27:55
我得到了'[10,20,30,[1,2,3,4]]'。 – icktoofay 2011-05-31 07:28:16
我現在已經更新了正確的代碼!這是給我的令人驚訝的O/P – 2011-05-31 07:34:55