2015-11-06 58 views
-1

当前学习鸭子打字,但我似乎无法弄清楚如何正确使用类。我觉得自己像个白痴。我究竟做错了什么?我似乎无法弄清楚如何将JSON对象传递给驻留在不同模块中的类

这是我的主文件:

if __name__ == '__main__': 
    result = api.get_result(api.build_search_url(getLocations(getNumLocations()))) 
    getSteps(getNumSteps()) 
    api.GrabSteps.calculate(result) 

而且在api.py类:

class GrabSteps: 

    def __init__(self, json_result): 
     self._json_result = json_result 

    def calculate(self, json_result): 
     print('DIRECTIONS') 
     x = 0 
     steps = len(json_result['route']['legs']) 
     print(steps) 
     while x < steps: 
      for item in json_result['route']['legs'][x]['maneuvers']: 
       print(item['narrative']) 
      x += 1 

我已经确定了可变result被妥善保存。

回答

0

至少有一个问题是这样的:GrabSteps是一类,所以你需要调用calculate之前进行实例化:

gs = api.GrabSteps(result) 
gs.calculate(result) 

我不是很确定你应该在传递什么样的参数两者。 __init__calculate采取json结果,但我不知道你是否应该通过相同的结果或不。问题的核心是,你需要在使用它来计算一些东西之前实例化这个类。

相关问题