python inheritence在覆盖后调用new url

时间:2015-08-09 11:25:04

标签: python

在下面的代码中,即使重写了init函数,它仍然会调用旧的url,我想要新的url内容而不是如何做到这一点

  import urllib
  import json


  class process():
     def processdata(self):
        for di in results:

           super(processcontent,self).__init__(new_url)
           new_obj  = getcontent.getdata()
           print new_obj
           break



  if __name__ == '__main__':
     url = "someurl"
     p = processcontent(url)
     #print p.getdata()
     p.processdata()

2 个答案:

答案 0 :(得分:1)

首先,您不应在初始化后调用__init__,其次,您不使用属性self.url,而是使用全局变量url。 由于url不是真正的属性,因此您应使用参数getdata调用url

  import urllib
  import json



  class processcontent(Basecontent,getcontent):
     def processdata(self):
        obj = self.getdata(self.url)
        results = obj["response"]
        for di in results:
           new_url =  di["clusterUrl"]
           new_obj  = self.getdata(new_url)
           print new_obj
           break

  def main():
     url = "someurl"
     p = processcontent(url)
     #print p.getdata()
     p.processdata()


  if __name__ == '__main__':
      main()

答案 1 :(得分:-1)

 class getcontent(object):
     @classmethod
     def getdata(self):
        response = urllib.urlopen(url)  #  <--- module-level variable
        content = json.loads(response.read())
        return content

您正在url方法中引用模块级变量getdata()