[問題] 英文實在看不懂,能不能請各位幫忙解釋?

看板Python作者 (ResolaQQ)時間8年前 (2016/02/03 20:38), 編輯推噓2(203)
留言5則, 2人參與, 最新討論串1/3 (看更多)
constants all module globals are considered constants. Their binding must not be changed at run-time. Moreover, global (i.e. prebuilt) lists and dictionaries are supposed to be immutable: modifying e.g. a global list will give inconsistent results. However, global instances don't have this restriction, so if you need mutable global state, store it in the attributes of some prebuilt singleton instance. 按照上面的說法,我本來以為可以這樣用 class Global_State: def __init__(self): initialize_other_library() self.global_mutable_variable = 0xFF global_state = Global_State() 但是實際上的結果很謎,大概是 1. initialize_other_library 這個函式根本沒有被呼叫 2. global_mutable_variable 可以使用,而且其值確實一開始為 0xFF 3. 有時候程式會當掉 據推測,我懷疑 __init__ 根本沒有被呼叫,如果在 main 裡面呼叫 __init__ 的話 1. initialize_other_library 確實有被呼叫 2. global_mutable_variable 可以使用,而且其值確實一開始為 0xFF 3. 程式不會當掉 想請問,按照那段英文,到底允不允許這樣產生一個 module global instance 供使用? 如果允許,為什麼我要自己呼叫 __init__? 英文太爛,實在抱歉 -- 放著養蚊子的部落格 http://resolaqq.blogspot.tw -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.46.93.156 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1454503100.A.9D4.html

02/03 22:33, , 1F
你寫的這段沒看到singleton(單例模式)的應用
02/03 22:33, 1F

02/03 22:41, , 2F
總之這英文在講全域變數不要亂動, 動了不知道會怎樣
02/03 22:41, 2F

02/03 22:41, , 3F
真的要改就用單例模式去改它
02/03 22:41, 3F

02/04 00:26, , 4F
全改單例又要大修了...
02/04 00:26, 4F

02/04 01:45, , 5F
跟python import module的細節有關吧, 乖乖用單例吧
02/04 01:45, 5F
文章代碼(AID): #1MiVIydK (Python)
討論串 (同標題文章)
文章代碼(AID): #1MiVIydK (Python)