Discuss / Python / 练习

练习

Topic source
class DefaultSaxHandler(object):
    def __init__(self, result):
        self.result = result
    def start_element(self, name, attrs):
        if 'yweather:location' == name:
            self.result['city'] = attrs['city']
            self.result['forecast'] = []
        if 'yweather:forecast' == name:
            L = self.result['forecast']
            L.append({'date':attrs['date'], 'high':attrs['high'], 'low':attrs['low']})

def parseXml(xml_str):
    result = {}
    handler = DefaultSaxHandler(result)
    parser = ParserCreate()
    parser.StartElementHandler = handler.start_element
    parser.Parse(xml_str)
    return result

问一下parseXml函数里,result变量是怎么被修改的,感觉它初始化之后并没有被赋值啊

它被传递进入DefaultSaxHandler里了,这里面赋值的


  • 1

Reply