简体   繁体   English

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

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

Currently learning duck typing, but I can't seem to figure out how to use classes properly.目前正在学习鸭子打字,但我似乎无法弄清楚如何正确使用课程。 I feel like an idiot.我觉得自己像个白痴。 What am I doing wrong?我究竟做错了什么?

Here's my main file:这是我的主要文件:

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

And the class in api.py:以及 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

I have made sure that the variable result is being stored properly.我已经确保变量result被正确存储。

At least one problem is this: GrabSteps is a class, so you need to instantiate it before calling calculate :至少有一个问题是: GrabSteps是一个类,因此您需要在调用calculate之前实例化它:

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

I'm not entirely sure what arguments you're supposed to pass in. Both the __init__ and calculate take a json result, but I don't know if you should be passing in the same result or not.我不完全确定您应该传入哪些参数。 __init__calculate采用 json 结果,但我不知道您是否应该传入相同的结果。 The core of the problem is, you need to instantiate the class before using it to calculate something.问题的核心是,您需要先实例化该类,然后再使用它来计算某些东西。

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

相关问题 我似乎无法弄清楚如何更新 tkinter 标签 - I can't seem to figure out how to update tkinter labels 似乎无法弄清楚为什么我无法将JSON读取到DataFrame中 - Can't seem to figure out why I can't read JSON into DataFrame 我似乎无法在 Python/Tkinter 中将 object 传递给另一个 class - I can't seem to pass on an object to another class in Python/Tkinter 我试图了解类和函数,但似乎无法弄清楚我的代码出了什么问题 - I am trying to understand class and function and can't seem to figure out what is wrong with my code 无法弄清楚如何将JSON附加到单个父对象中 - Can't figure out how to append JSON into a single parent object 我不知道如何处理不同的错误 - I can't figure out how to handle different errors 似乎无法弄清楚如何用Python编写循环 - Can't seem to figure out how to write a Loop in Python 我似乎无法弄清楚如何通过嵌套循环显示某些内容 - I can't seem to figure out how to display something through nested loops 我似乎无法弄清楚如何生成多个随机数等于10分 - I can't seem to figure out how to generate multiple random numbers to equal a score of 10 混淆,因为我无法弄清楚是什么改变了 python 中 class 的 object 的属性 - confusion because i can't figure out what is changing the attribute of the object of a class in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM