简体   繁体   English

Python函数在elif或else中返回None

[英]Python function returns None in elif or else

I have the variable r of type dict.我有 dict 类型的变量 r。 Variable item holds a list of items, and total_results is an int.变量 item 包含一个项目列表,total_results 是一个 int。 As long as the condition is met that the number of items is less than the total amount of items, the function is called recursively.只要满足项目数小于项目总数的条件,就递归调用该函数。 However, if I test whether the items are equal to the total results, the return statement returns None.但是,如果我测试项目是否等于总结果,则返回语句返回 None。 Changing this elif to if does return the correct value.将此 elif 更改为 if 确实会返回正确的值。

Could someone point me into the right direction to find out why returning r yields None in the elif or else block?有人能指出我正确的方向来找出为什么在 elif 或 else 块中返回 r 会产生 None 吗?

Many thanks !!非常感谢!!

Relevant code snippet:相关代码片段:

if len(items) < total_results:
    params['start_index'] = len(items)

    self.fetch_company_officers(company_number=company_number, items=items, **params)

elif len(items) == total_results:
    r['items'] = items
    return r

Full code:完整代码:

def fetch_company_officers(self, company_number, items=None, **kwargs):
    uri = 'company/{}/officers'.format(company_number)
    params = kwargs

    r = self.make_request(uri=uri, **params)

    # Test if items are set
    if items is None:
        items = r['items']
    else:
        items.extend(r['items'])

    # Iterate multiple pages
    total_results = r['total_results']

    if len(items) < total_results:
        params['start_index'] = len(items)

        self.fetch_company_officers(company_number=company_number, items=items, **params)

    elif len(items) == total_results: # TO DO: ??
        r['items'] = items
        return r

Your function is missing handling logic for the following scenario to len(items) > total_results .您的函数缺少以下场景的处理逻辑len(items) > total_results

So the function is reaching end of line and returning None by default所以函数到达行尾并默认返回 None

I think I understand what you're trying to do, but recursion is not the way to go here.我想我明白你想要做什么,但递归不是这里的方法。 You want to keep getting the items from the next page for as long as ( while ) the number of items you've retrieved smaller than the total number of results you expect.只要 ( while ) 您检索的项目数小于您期望的结果总数,您就希望继续从下一页获取项目。 So.. lets use a while loop!所以..让我们使用一个while循环!

This is my version of your code.这是我的代码版本。 Note that I've not been able to test this since I don't have the rest of your code.请注意,我无法对此进行测试,因为我没有您的其余代码。

def fetch_company_officers(self, company_number, **params):

    # Start with an empty list of items
    items = []

    # Make the first request
    uri = f"company/{company_number}/officers"
    response = self.make_request(uri=uri, **params)

    # Extract information from the first batch
    items += response.get("items", [])
    total_results = response.get("total_results")

    # Stop iterating when we don't know the total number of results to expect
    if total_results is None:
        return items

    # Keep getting more results until our list of items
    # is as long as the total number of results we expect
    while len(items) < total_results:

        # Get the next page
        response = self.make_request(uri=uri, start_index=len(items), **params)

        # Extract information from this batch
        items += response.get("items", [])

    # We've now retrieved everything
    return items

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

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