[英]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.