→ IAMPF: 因為你是送了reference過去 07/16 09:59
推 TitanEric: python是call by object list相當於傳reference 07/16 10:16
先謝謝版友們的回答,剛剛也google後才大致了解
pass by reference跟pass by value之別
所以看起來python是根據不同情況去pass value或reference囉?
那解決之道就是在函式內又在設一個變數嗎?
以上例來說,應修正為
def xxx(d):
y=d
y[0]=123
return y
※ 編輯: jasonhsu14 (211.22.64.145 臺灣), 07/16/2020 10:23:52
※ 編輯: jasonhsu14 (211.22.64.145 臺灣), 07/16/2020 10:26:31
推 s0914714: 要改成y=d[:] 一般來說要傳list會寫成 def xxx(d=None): 07/16 10:52
→ kenduest: 用 y=d[:] 複製一份,或是你要用 y=copy.copy(d) 07/16 10:52
→ s0914714: 函式裡面判斷if not d成立 d = [] 07/16 10:53
推 bugbug777: shalow/deep copy, reference 07/16 10:55
→ as30385438: python中alway是pass by pointer,不會有不同情況 07/18 01:27