“ yield”关键字有什么做用? - What does the “yield” keyword do?

问题:

What is the use of the yield keyword in Python? Python中yield关键字的用途是什么? What does it do? 它有什么做用? node

For example, I'm trying to understand this code 1 : 例如,我试图理解这段代码1 app

def _get_child_candidates(self, distance, min_dist, max_dist):
    if self._leftchild and distance - max_dist < self._median:
        yield self._leftchild
    if self._rightchild and distance + max_dist >= self._median:
        yield self._rightchild

And this is the caller: 这是呼叫者: this

result, candidates = [], [self]
while candidates:
    node = candidates.pop()
    distance = node._get_dist(obj)
    if distance <= max_dist and distance >= min_dist:
        result.extend(node._values)
    candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
return result

What happens when the method _get_child_candidates is called? 调用_get_child_candidates方法时会发生什么? Is a list returned? 是否返回列表? A single element? 一个元素? Is it called again? 再叫一次吗? When will subsequent calls stop? 后续通话什么时候中止? spa


1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. 1.这段代码是由Jochen Schulz(jrschulz)编写的,Jochen Schulz是一个很好的用于度量空间的Python库。 This is the link to the complete source: Module mspace . 这是完整源代码的连接: Module mspace .net


解决方案:

参考一: “ yield”关键字有什么做用?
参考二: What does the “yield” keyword do?
相关文章
相关标签/搜索