我想學習Python和我堅持與類之間的共享變量。從主類訪問變量
我有一個類,設置一個變量是這樣的:
class CheckStuff(threading.Thread):
def __init__(self, debug):
threading.Thread.__init__(self)
self.debug = debug
self.newGames = False
def get_new_games(self):
return self.newGames
def set_new_games(self, new_games):
self.newGames = new_games
def run(self):
# DO STUFF #
# ... #
self.set_new_games(True)
return
我想從我的主要訪問new_games
,我想是這樣的:
if __name__ == "__main__":
debug = True
t1 = cs.CheckStuff(debug)
t1.start()
t1.join()
print(cs.CheckStuff(debug).get_new_games())
exit()
但這總是返回False
。我錯在哪裏?任何提示讚賞
我可以問一個問題嗎?什麼是「cs」? –
它是從頂部的導入:'import CheckStuff as cs' – PrimuS
好的謝謝,下次你可以添加這個簡單的行或刪除「cs」來避免這種情況。最好的問題是,如果我可以將代碼粘貼到我的IDE中並運行它,而不搜索未定義的方法和變量以刪除它們: –