考慮下面的類:如何自我傳遞給構造
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