[問題] 新手請教關於參數的傳遞

看板Python作者 (小肉...)時間13年前 (2012/02/22 17:41), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串1/1
最近在研究python,可是一直有個小問題沒解決 因此想請教各位前輩,如何解決小弟日思夜想的問題 問題: abc.py temp = 1 def settemp(self) global temp temp = 2 abc2.py from xx.abc import temp def reviewtemp(self) print temp 螢幕上顯示的temp=1,不過我想得到的值是temp=2,也就是settemp()執行完後 的temp值,想請教各位前輩,有什麼方法可以取得abc.py下settemp()後的temp值呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.110.101.31 ※ 編輯: wenyan520 來自: 140.110.101.31 (02/22 17:42)

02/22 17:45, , 1F
from xx.abc import settemp
02/22 17:45, 1F

02/22 17:51, , 2F
以上請忽略...
02/22 17:51, 2F

02/22 17:51, , 3F
但我覺得你應該用return來傳值, 而不是用global
02/22 17:51, 3F

02/22 19:11, , 4F
你沒有執行settemp()阿
02/22 19:11, 4F

02/25 12:14, , 5F
global is poison...
02/25 12:14, 5F

02/27 21:13, , 6F
用return傳,因為python在區域間是用副本
02/27 21:13, 6F
文章代碼(AID): #1FHBX846 (Python)
文章代碼(AID): #1FHBX846 (Python)