Python 3,在多个模块中使用的一类 [英] Python 3, one class used across multiple modules
本文介绍了Python 3,在多个模块中使用的一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在多个模块中使用以下类,而无需在每个模块中使用 log = InitLog()
。我需要能够在多个模块中使用一个定义的类变量(在本示例中为 log
)。在不彻底更改当前代码的情况下,最好的方法是什么。谢谢
I would like to use the following class across multiple modules without needing log = InitLog()
in every module. I need to be able to use ONE defined class variable (in this example log
) across multiple modules. What is the best way to do this without altering my current code too drastically. Thanks
import os
import sys
import pdb
import fileinput
import Tools
class InitLog:
def __init__(self):
pass
def setLaws(self):
self.sound = 'off'
self.engine = 'goo.txt'
def Update(self):
while Tools.Locked.State.LogAddress == True: pass
Tools.Locked.State.LogAddress = True
try: os.remove(path + '/' + self.dest + '/init.log')
except: pass
summery = 'sound: ' + self.sound + '\n'
summery += 'engine: ' + self.engine + '\n'
path = os.getcwd()
if not os.path.exists(self.dest): os.makedirs(self.dest)
if os.path.isfile(path + '/' + self.dest + '/init.log') == True: os.remove(path + '/' + self.dest + '/init.log')
with open (path + '/' + self.dest + '/init.log', mode='a', encoding='utf-8') as a_file:
a_file.write(summery)
Tools.Locked.State.LogAddress = False
Tools.Locked.State.LogAddress = False
log = InitLog()
log.setLaws()
log.sound = 'on'
log.Update()
推荐答案
创建一个名为 logging
的模块,其中包含 log
。在其他模块中,使用来自日志导入日志
。
Create a module called logging
which contains log
. In other modules use from logging import log
.
这篇关于Python 3,在多个模块中使用的一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文