繁体   English   中英

我似乎无法弄清楚如何将 JSON 对象传递给驻留在不同模块中的类

[英]I can't seem to figure out how to pass a JSON object to a class residing in a different module

目前正在学习鸭子打字,但我似乎无法弄清楚如何正确使用课程。 我觉得自己像个白痴。 我究竟做错了什么?

这是我的主要文件:

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被正确存储。

至少有一个问题是: GrabSteps是一个类,因此您需要在调用calculate之前实例化它:

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

我不完全确定您应该传入哪些参数。 __init__calculate采用 json 结果,但我不知道您是否应该传入相同的结果。 问题的核心是,您需要先实例化该类,然后再使用它来计算某些东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM