我有以下情况:
class A():
def __init__(self, log = True):
self.log = log
def __call__(self):
if self.log:
self.log ='\n' # whatever no-Null string to reinitialize the log attribute at each call
do_things()
self.log += 'I did things'
class B()
def __init__(self):
self.a = A(log = True)
self.log_master = []
def __call__(self):
for i in range(num):
self.a()
self.log_master.append(a.log)
self.log_master.append('other things')
save_to_file(self.log_master)
所以我有一个类B
,它是用类A
的实例初始化的B
调用A
。调用类A
时,它初始化一个字符串,该字符串用作记录操作的容器。调用结束时,B
检查A
的日志字符串,并将其附加到log_master
以及其他内容。在一切结束时log_master
被保存到文件中。基本上我有两个类,其中一个用作另一个实例的容器。双方在编写日志文件时“协作”
我能感觉到这种方法的恐怖。我的A
代码被难看的“if self.log:…”弄得乱七八糟。生成一个像样的、可定制的日志文件的正确方法是什么
目前没有回答
相关问题 更多 >
编程相关推荐