对象具有字典列表:
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
为什么这是可能的,我错过了什么吗?你知道吗
===更新
完成下面的演示代码。我找出了错误的原因。这是“德尔”的台词自聚合器". 当我删除这一行时,一切正常。在Windows7上用Python2.7.10和Python2.7.11(32位)在OSX上测试了这种行为。你知道吗
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)
您将
aggregator
定义为一个类属性;除非您直接指定给self.attribute
,否则它是一个共享的类属性。但当你这么做的时候:自动激活实例属性。因此,在这一点之后,它是一个完全不同的变量,用于隐藏class属性。除非执行
del self.aggregator
,否则删除instance属性,并取消class属性的阴影,以查看原始(共享)值。你知道吗如果目标是拥有实例属性,请使用实例属性。更改:
收件人:
它将仅仅是一个实例变量(只是不要
del
实际的属性,如果您打算稍后使用它,那是毫无意义的;但是分配空的list
是可以的)。你知道吗注意,在
self.aggregator
引用的list
(del self.aggregator[:]
或Py3.3+,self.aggregator.clear()
)引用的list
可能有多个引用的情况下,分配空的list
(替换self.aggregator
中保持的引用)和删除list
的内容是有区别的。更多细节请看Clearing Python lists。你知道吗相关问题 更多 >
编程相关推荐