我是在 python 中自学的,需要一些我正在尝试编写的非常简单的课程的帮助。我想在各种模块中使用 InitLog 类之外的所有内容,而无需在每次使用它时声明一个新的类变量(例如,我不想在我希望使用该类的每个(众多)模块中声明 betty = Initlog .. . 有什么办法可以做到这一点吗?在此先感谢
import os
import sys
import pdb
import fileinput
import Tools
class InitLog:
def __init__(self):
pass
def Update(self):
self.sound = sound
self.engine = engine
def save(self):
self.summery = 'sound: ' + self.sound + '\n'
self.summery += 'engine: ' + self.engine + '\n'
#lock for use within threads
Tools.Locked.State.LogAddress = False
while Tools.Locked.State.LogAddress == True: pass
Tools.Locked.State.LogAddress = True
try: os.remove(path + '/' + self.dest + '/init.log')
except: pass
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(self.summery)
Tools.Locked.State.LogAddress = False
InitLog.Update.sound = 'on'
InitLog.Update.engine = 'google.txt'
InitLog.save()
错误:TypeError: save() takes exactly 1 positional argument (0 given)
编辑:
我已经缩短了上面的示例以减少冗余。
这是我以前使用的工作代码(在尝试使用 InitLog 而不声明新的类变量的原始请求之前)
import os
import sys
import pdb
import fileinput
import Tools
class InitLog:
def __init__(self):
pass
def setLaws(self):
self.sound = 'off'
self.engine = 'google.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()
我不想log = InitLog()
为我需要使用这个类的每个模块说