在for循环中重新初始化时不会覆盖Python对象

在for循环中重新初始化时不会覆盖Python对象,python,object,for-loop,initialization,Python,Object,For Loop,Initialization,所以我有一节课: class SUSTAIN: def __init__(self, r, beta, d, threshold, learn): self.clusters = [] self.activations = [] self.connections = [] self.catunitacts = [] self.coutputs = [] self.maxValue = 0

所以我有一节课:

class SUSTAIN:

    def __init__(self, r, beta, d, threshold, learn):

        self.clusters = []
        self.activations = []
        self.connections = []
        self.catunitacts = []
        self.coutputs = []

        self.maxValue = 0.0
        self.minValue = 0.0

    def stimulate(self, item, env):
        #code
        return [response, probofcorrect, outputprobs, self.catunitacts, self.activations, self.distances]

    def learn(self, item, env):
        #code
        return [self.LAMBDAS, self.connections, self.clusters, int(maskclus[3][1]), accuracy, len(self.clusters)]
我想在下面的函数中初始化一个对象100次:

def testing(data):
    subjectdata = []
    subjectdata2 = []
    #loop1
    for i in range(100):
        model = SUSTAIN(r = 0.0, beta = 5.386305, d = 5.0,  
            threshold = 0.89, learn = 0.09361126)
        #loop2
        for k in range(4):
            #loop3
            for j in trainingblock:
                trialn = int(floor(j[0][1]))
                [res, prob, outunits, outacts, act, dist] = model.stimulate(j, env)
                [lambdas, clus, conn, response, accuracy, nclus] = model.learn(j, env)
                trialdata = [response, accuracy, nclus]
                subjectdata.append(trialdata)
        #loop4
        for k in dataitems:
            trialn = int(floor(k[0][1]))
            [res, prob, outunits, outacts, act, dist] = model.stimulate(k, env)
            [lambdas,  clus,  conn,  response,  n] = model.learn(k, env)
            trialdata = [trialn, response, n]
            subjectdata2.append(trialdata)
    write_file("training", directory1, subjectdata, ',')
    write_file("generalization", directory2, subjectdata2, ',')
出于某种奇怪的原因,函数中的loop4不断影响loop2的结果,尽管它们彼此完全独立,并且每次在loop1中都会初始化一个新实例。这就像python记住对象而不删除它一样。我知道loop2会影响loop4的结果,这是故意的,但相反的情况很奇怪。

您在loop2和loop4中使用的是相同的模型。此模型包含列表

    self.clusters = []
    self.activations = []
    self.connections = []
    self.catunitacts = []
    self.coutputs = []

这也是刺激和学习的回报。对于每个模型,每个列表只有一个实例。在任何地方更改其中一个列表的内容,就会在任何地方更改它。

请将代码示例减少到再现问题所需的最小值。通过这样做,你甚至可以自己解决问题。如果你不解决问题,别人会更容易关注重要的部分。是的,但这不是问题所在。问题是,当我在loop1中再次初始化模型时,loop2中新初始化的模型的行为就像是以前初始化的同一个实例,而不是新实例。我尝试在loop1的末尾将模型设置为空字符串,但没有任何更改。我正在执行常规数组操作并为模型的参数赋值。Env只是我在函数中使用的一个掩蔽数组,没有足够的资源来复制它。当你建立模型时会发生什么?