考虑下面的类:如何自我传递给构造
class WebPageTestProcessor:
def __init__(self,url,harUrl):
self.url = url
self.harUrl = harUrl
def submitTest():
response = json.load(urllib.urlopen(url))
return response["data"]["testId"]
def main():
wptProcessor = WebPageTestProcessor("http://www.webpagetest.org/runtest.php?f=json&priority=6&url=http%3A%2F%2Fwww.target.com%2F&runs=3&video=1&web10=0&fvonly=1&mv=1&k=d3ebfa90cb4e45a9bb648e7ebf852321")
print wptProcessor.submitTest()
if __name__ =='__main__':main()
在运行时,它抛出一个错误说:
TypeError: __init__() takes exactly 3 arguments (2 given).
我通过None
作为参数:
wptProcessor = WebPageTestProcessor(None,"http://www.webpagetest.org/runtest.php?f=json&priority=6&url=http%3A%2F%2Fwww.target.com%2F&runs=3&video=1&web10=0&fvonly=1&mv=1&k=d3ebfa90cb4e45a9bb648e7ebf852321")
然后它说:
TypeError: submitTest() takes no arguments (1 given)
有谁知道如何将self
传递给构造函数?
你没有通过'harUrl',你需要将自己添加到'submitTest(self)' – 2014-11-03 14:50:34