Python在不需要时返回数据

2024-04-25 01:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用prawreddit库从reddit提取数据,我遇到了这样一段代码,我不明白它为什么返回任何数据(在BaseReddit(full source)类中):

def get_content(self, page_url, limit=0, url_data=None, place_holder=None,
                root_field='data', thing_field='children',
                after_field='after'):
    """A generator method to return reddit content from a URL. Starts at
    the initial page_url, and fetches content using the `after` JSON data
    until `limit` entries have been fetched, or the `place_holder` has been
    reached.

    :param page_url: the url to start fetching content from
    :param limit: the maximum number of content entries to fetch. If
        limit <= 0, fetch the default_content_limit for the site. If None,
        then fetch unlimited entries--this would be used in conjunction
        with the place_holder param.
    :param url_data: dictionary containing extra GET data to put in the url
    :param place_holder: if not None, the method will fetch `limit`
        content, stopping if it finds content with `id` equal to
        `place_holder`.
    :param data_field: indicates the field in the json response that holds
        the data. Most objects use 'data', however some (flairlist) don't
        have the 'data' object. Use None for the root object.
    :param thing_field: indicates the field under the data_field which
        contains the list of things. Most objects use 'children'.
    :param after_field: indicates the field which holds the after item
        element
    :type place_holder: a string corresponding to a reddit content id, e.g.
        't3_asdfasdf'
    :returns: a list of reddit content, of type Subreddit, Comment,
        Submission or user flair.
    """
    content_found = 0

    if url_data is None:
        url_data = {}
    if limit is None:
        fetch_all = True
    elif limit <= 0:
        fetch_all = False
        limit = int(self.config.default_content_limit)
    else:
        fetch_all = False

    # While we still need to fetch more content to reach our limit, do so.
    while fetch_all or content_found < limit:
        page_data = self.request_json(page_url, url_data=url_data)
        if root_field:
            root = page_data[root_field]
        else:
            root = page_data
        for thing in root[thing_field]:
            yield thing
            content_found += 1
            # Terminate when we reached the limit, or place holder
            if (content_found == limit or
                place_holder and thing.id == place_holder):
                return
        # Set/update the 'after' parameter for the next iteration
        if after_field in root and root[after_field]:
            url_data['after'] = root[after_field]
        else:
            return

在我看来,所有return语句都没有参数,因此默认返回None。有人能给我解释一下吗?在

注意:代码是python2.x


Tags: thetononeurlfielddataparampage
2条回答

这是一个生成器函数,可以通过yield语句来判断。值实际上是“返回”的,而不是实际从函数返回。当从函数中请求另一个值时,生成器从它生成的点恢复(根据下面的代码,继续for thing循环…)。在

for thing in root[thing_field]:
    yield thing

简单示例:

^{pr2}$

它是一个发电机。请参阅yield语句以获取这方面的提示。在

http://wiki.python.org/moin/Generators

相关问题 更多 >