【发布时间】:2019-04-14 13:25:06
【问题描述】:
在这个函数中,如果它不为空,我想返回一个列表。要检查列表是否为空,我使用if not data: 并测试它是否填充了我使用elif data: 但是当len(data) 等于13 时不会执行return 语句。可能是什么原因?
当列表为空时,使用新的start 和end 参数再次调用函数,直到data 被填充。
Class MyClass:
def downloadHistoryOHLC(url, start, end):
http = urllib.request.urlopen(url)
data = json.loads(http.read().decode())
print('length is', len(data)) # Here I test if list is filled
if not data:
''' Add 366 days to start date if list is empty '''
start = datetime.strptime(start, '%Y-%m-%dT%H:%M:%SZ') + timedelta(days=366)
start = str(start.isoformat()+'Z')
end = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ') + timedelta(days=366)
end = str(end.isoformat()+'Z')
MyClass.downloadHistoryOHLC(url, start, end) # if list is empty I execute the same function with new parameters
elif data:
return data
当我执行该函数时,我可以看到列表的长度为 13,但没有返回任何数据。
In [844]: mylist = MyClass.downloadHistoryOHLC(start, end, coin_id, name)
length is 0
length is 0
length is 0
length is 0
length is 13
In [845]: mylist
In [846]:
【问题讨论】:
-
您错过了 MyClass.downloadHistoryOHLC() 中返回值的返回。它应该像“return MyClass.downloadHistoryOHLC(...”
标签: python python-3.x