[英]return value of recursive function python
I have a problem with this recursive function: 我有这个递归函数的问题:
def query(params,conta):
req = api.APIRequest(site, params)
res = req.query(querycontinue=False)
pprint.pprint(res)
conta=conta+str(res).count('title')
print conta
if 'query-continue' not in res:
return conta
else:
parametri=params.copy()
lastContinue=res['query-continue']
lastContinue=lastContinue['links']
lastContinue=lastContinue['gplcontinue']
parametri['gplcontinue']=lastContinue
query(parametri,conta)
paramet = {'action':'query',
'pageids':'44776',
'generator':'links',
'gpllimit':'max'
}
x=query(paramet,0)
print x
It return the correct value if it never execute the else block. 如果它从不执行else块,则返回正确的值。 Instead, if it execute at least one time the else block, then it return always None
. 相反,如果它至少执行一次else块,那么它总是返回None
。 Why? 为什么?
You are ignoring the return value of the recursive call. 您忽略了递归调用的返回值。 You still need to return what the recursive call to query()
returns explicitly: 您仍然需要返回对query()
的递归调用显式返回的内容:
else:
parametri=params.copy()
lastContinue=res['query-continue']
lastContinue=lastContinue['links']
lastContinue=lastContinue['gplcontinue']
parametri['gplcontinue']=lastContinue
return query(parametri,conta)
otherwise the outer invocation of query()
just ends and returns the default value, which is None
. 否则query()
的外部调用结束并返回默认值,即None
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.