下面的代码,执行有错:
class TripleDataFilter(): def __init__(self): self.idFilter = None self.propFilter = None self.valueFilter = None self.prefilter = None self.filterOrder = [] self.filterMap = {"id":self.idFilter, "prop_name":self.propFilter, "prop_value":self.valueFilter} def read_conf(self, confFile): if (not os.path.isfile(confFile)) or "" == confFile: sys.stderr.write(confFile + " is not exit!\n") return False config = ConfigParser.ConfigParser() config.read(confFile) if "WORK_DIR" in config.sections(): workDir = config.items("WORK_DIR")[0][1].strip() else: workDir = "." prefilterConf = config.items("PREFILTER")[0][1] if "none" != prefilterConf.lower(): self.preFilter = prefilter.PreFilter() self.preFilter.read_conf(prefilterConf) fItemList = config.items("FILTER_CONF_FILES") for fItem in fItemList: segName = fItem[0].strip() filePath = fItem[1].strip() if os.path.split(filePath)[0] == "": filePath = workDir + "/" + filePath if "id_conf" == segName: self.idFilter = SegmentFilter("id"); self.idFilter.read_conf(filePath, self.preFilter) elif "prop_conf" ==segName: self.propFilter = SegmentFilter("prop"); self.propFilter.read_conf(filePath, self.preFilter) elif "value_conf" == segName: print "conf value" self.valueFilter = SegmentFilter("value"); self.valueFilter.read_conf(filePath, self.preFilter) else: sys.stderr.write("字段名错误 %s\n" % segName) return False self.filterOrder = config.items("FILTER_ORDER") self.filterOrder.sort(key = lambda x:x[1]) def filter(self, tripleExt): for filterType in self.filterOrder: if self.filterMap[filterType[0]]: self.filterMap[filterType[0]].filter(tripleExt) else: print filterType[0] + " is null"
执行filter时,会说filterMap里的元素都为None。
原因是,创建dict 时,会重新复制一份新的对象到dict中。
最开始,所有的filter都为None,存入了dict中。
后来,虽然filter都被赋值,但dict中的值没有改变,仍为None。