【发布时间】:2013-11-19 13:28:42
【问题描述】:
我有时想“重置”一门课程。与其手动清除类中的所有变量及其使用的所有模块,我认为通过调用自身的 init 来重建它可能是个好主意。我担心的是我不太确定这是否是一个好的模式,或者 GC 是否正确清除了旧对象。
一个例子如下:
from modules import SmallClass
from modules import AnotherClass
class BigClass(object):
def __init__(self, server=None):
"""construct the big class"""
self.server = server
self.small_class = SmallClass(self.server)
self.another_class = AnotherClass(small_class)
def reset_class(self):
"""reset the big class"""
self.__init__(self.server)
这会导致问题,还是有更好的方法来解决这个问题?
【问题讨论】:
标签: python constructor garbage-collection reset init