python - 我应该如何正确记录我的类容器?
问题描述
我有以下情况:
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)
所以我有一个用 classB
实例初始化的类A
。B
来电A
。当调用该类时,A
它会初始化一个字符串,该字符串用作记录操作的容器。通话结束时,B
检查A
' 的日志字符串并将其附加到log_master
,除其他事项外。最后,所有内容log_master
都保存到文件中。基本上我有两个类,其中一个用作另一个实例的容器。两者都在编写日志文件时“合作”。
我能感受到这种方法的恐怖。我的A
代码中充满了丑陋的“if self.log: ..”。生成体面且可定制的日志文件的正确方法是什么?
解决方案
推荐阅读
- bash - 将从 shell 脚本接收的参数传递给同一 shell 脚本内的 db2 查询
- git - 致命:肮脏的存储库:有未提交的更改。使用 git ftp push 在管道中退出...
- python - 是否有一种数据类型/结构可以:保存数字列表、可散列、无序并允许重复
- ruby-on-rails - 如何在 ruby 中将字符串、整数、日期等转换为可查询的格式?
- oracle - 在 oracle 中有查看对象编号吗?
- python - 不同颜色的绘图的不同部分
- imagemagick - 如何使用具有特定输出文件名格式的 ImageMagick 将 MTIFF 转换为 PNG?
- rust - 有没有办法在 Rust 中用“0x”正确证明十六进制数?
- python - 减少循环执行时间python
- c# - Unity3d中使用MySQL,填充数据表