我想学习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中并运行它,而不搜索未定义的方法和变量以删除它们: –