看板 Python 關於我們 聯絡資訊
書上的實作如下: 0;32m###################### def diplayInventory(inventory): #顯示字典用的函數 print("Inventory:") item_total=0 for k,v in inventory.items(): print(str(v)+' '+k) item_total=item_total+1 print("items number:"+str(item_total)) def addInventory(inventory,addItems): #這邊是要練習寫的地方 inv={'rope':1,'劍':1,'gold coin':42,'dagger':1,'arrow':1} dragonLoot=['超級劍','gold coin','arrow'] inv=addInventory(inv,dragonLoot) diplayInventory(inv) ###################### 其中inv=addInventory(inv,dragonLoot)的參照部分我不明白 因為函數如果這樣寫就可以: ###################### def addInventory(inventory,addItems): for i in range(len(addItems)): inv.setdefault(addItems[i],0) #串列名稱key寫入inv inventory[addItems[i]]=inventory[addItems[i]]+1 #增加串列名稱i到字典內的數量 inv={'rope':1,'劍':1,'gold coin':42,'dagger':1,'arrow':1} #倉庫 dragonLoot=['超級劍','gold coin','arrow'] addInventory(inv,dragonLoot) diplayInventory(inv) ###################### 書裡面卻是用參照的方式,但diplayInventory函數要傳入的應該是字典 參照的對象卻是一個把字典參照了函數,那這樣就會顯示錯誤訊息: Traceback (most recent call last): File "C:\Dropbox\Tools\python code\python.py", line 20, in <module> diplayInventory(inv) File "C:\Dropbox\Tools\python code\python.py", line 4, in diplayInventory for k,v in inventory.items(): AttributeError: 'NoneType' object has no attribute 'items' 想請教如果是按書裡的寫法應該是怎麼解? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.187.101.135 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1591285642.A.B15.html
TuCH: return inventory 06/05 00:09
kslman: 謝謝,所以在這邊其實inventory就是inv的參照,對嗎? 06/05 01:06
stlevi811101: https://onlinegdb.com/Bk_h6Gwn8 這樣? 06/05 09:20
謝謝,學到新寫法
stlevi811101: 也不用return吧 06/05 09:21
其實我的想寫法也是用addInventory(inv,dragonLoot),也可以運作 只是書上的範例就是寫要用:inv=addInventory(inv,dragonLoot) 所以才疑惑這樣要如何寫,這只是書上一個實作專題,而且還沒給答案=.=
stlevi811101: 猜你的問題是dict的global或是local的問題是嗎 06/05 11:40
其實書上也沒有寫這題是要考甚麼觀念=.=
mychiux413: inv=可以拿掉,你可以想像dict/list都是指標,str/int/ 06/05 12:21
恩恩,我自己想的版本也是inv=可以拿掉就可運作,但我不懂的就是書上為什麼這樣寫
mychiux413: /float等都是值,所以dict/list都有一個.copy方法 06/05 12:21
mychiux413: 雖然傳指標效能比較好,但在大型專案上會盡量避免 06/05 12:23
mychiux413: 沒有效能考量的話,我會用return obj.copy()的方式去傳 06/05 12:25
這個就比較深了,我好好琢磨琢磨 ※ 編輯: kslman (218.187.82.118 臺灣), 06/05/2020 23:06:27
alvinlin: 其實你的用法才是參照吧。主程式有個變數inv所以函數要 06/06 00:29
alvinlin: 有return,沒有return時,Python 有一個預設return Non 06/06 00:29
alvinlin: e。所以你的inv會變成None,所以就錯了。因為你的做法等 06/06 00:29
alvinlin: 於用inventory去修改了inv。所以你的inventory 參照了in 06/06 00:29
alvinlin: v的記憶體位址,當你一修改inventory 後inv的值也跟著改 06/06 00:29
alvinlin: 了,這是樓上的意思。但這個例子和參不參照沒什麼關聯。 06/06 00:29
kslman: 看懂了<(_ _)> 06/06 00:55
alvinlin: ^^。但你原意應該是指「參數」。因為函數通常是重復使用 06/06 01:24
alvinlin: 的。就像print()。有時候函數還要給別人用,所以最好是 06/06 01:24
alvinlin: 不要不傳參數直接修改主程式,這樣你的函數只能在這個程 06/06 01:24
alvinlin: 式用,未來要寫成套件時就不方便了 06/06 01:24
stlevi811101: https://onlinegdb.com/ry1luOK3U 06/07 04:09
stlevi811101: 如果不想修改原有的字典 可以在函式內另建空字典 06/07 04:10
stlevi811101: update裝載原字典 最後再把獨立的inv_dict吐出來 06/07 04:11
stlevi811101: 剛開始學python可以善用id() method去看變數地址 06/07 04:13
kslman: 收到! 06/08 22:12
salmon12706: 工研院人工智慧課程推薦https://reurl.cc/4RDRaK 06/29 16:16