无法清除词典列表

时间:2016-01-20 13:40:11

标签: python list dictionary reset

对象有一个词典列表:

self.aggregator = []
self.aggregator.append({'type': 'Log', 'entry': logline1})
self.aggregator.append({'type': 'Log', 'entry': logline2})
print self.aggregator

到目前为止一切正常:

[{'type': 'Log', 'entry': 'Content of logline 1'}, {'type': 'Log', 'entry': 'Content of logline 2'}]

然后我尝试使用不同的方法重置此列表的内容:

self.aggregator = []
del self.aggregator

现在,当我打印列表时,它是空的:

[]

但是当我检查列表的长度时,它仍然具有旧的大小:

>> print len(self.aggregator)
2

为什么这可能,我错过了什么?

===更新

完整的演示代码如下。我弄清楚导致错误的原因。它是" del self.aggregator"的路线。当我删除这一行时,一切都按预期工作。在Windows 7上使用Python 2.7.10和Python 2.7.11(32位)在OS X上测试了此行为。

class Checker(object):

    aggregator = []
    aggregator_max = 10

    def __init__(self):
        pass

    def finalizeAggregator(self):
        string = "\n".join([attribute['string'] for attribute in self.aggregator])

        # Check for a match on all the aggregator content
        match_result = self.checkString(string,"")

        if match_result:
            # If match has been found, check the aggregator contents one by one
            for element in self.aggregator:
                self.checkString(element["string"],
                                 element["module"],
                                 use_aggregator=False)

        # Clear aggregator
        print self.aggregator
        self.aggregator = None
        self.aggregator = []
        del self.aggregator
        print self.aggregator
        print len(self.aggregator)

    def checkString(self, string, module, use_aggregator=False):

            # If aggregator should be used
            if use_aggregator:
                print "USING aggregator"

                # As long as the aggregator is not full
                if len(self.aggregator) <= self.aggregator_max:

                    #if string not in self.aggregator:
                    # Add element to aggregator
                    self.aggregator.append({"string": string,
                                            "module": module})

                # Process aggregator if full
                if len(self.aggregator) >= self.aggregator_max:
                    self.finalizeAggregator()

                # Otherwise return
                else:
                    return False

            else:
                print "NOT using aggregator"

            if "evil" in string:
                print "WARNING!!!"

if __name__ == '__main__':
    checker = Checker()
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is evil', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)
    checker.checkString('This is benign', 'test', use_aggregator=True)

1 个答案:

答案 0 :(得分:2)

您将aggregator定义为类属性;直到您直接分配给self.attribute,它就是共享类属性。但是当你这样做时:

self.aggregator = None

自动生成实例属性。所以在那之后,它是一个完全不同的变量,它隐藏(隐藏)类属性。除非您执行del self.aggregator,否则将删除实例属性,并取消隐藏类属性,查看原始(共享)值。

如果目标是拥有实例属性,请使用实例属性。变化:

class Checker(object):

    aggregator = []
    aggregator_max = 10

    def __init__(self):
        pass

为:

class Checker(object):
    aggregator_max = 10

    def __init__(self):
        self.aggregator = []

并且它只是一个实例变量(如果你打算稍后使用它,那就不要del实际属性,这是无意义的;虽然分配空list也没问题。

请注意,分配空list(替换self.aggregator中保存的参考)和删除内容之间存在差异listself.aggregator或Py3.3 +,del self.aggregator[:])引用的self.aggregator.clear(),如果引用的list可能有多个引用按self.aggregator。有关详细信息,请查看Clearing Python lists