Python中yield
关键字有什么用?它有什么作用?
例如,我试图理解这段代码1:
def _get_child_candidates(self, distance, min_dist, max_dist):if self._leftchild and distance - max_dist < self._median:yield self._leftchildif self._rightchild and distance + max_dist >= self._median:yield self._rightchild
这是调用者:
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
调用方法_get_child_candidates
时会发生什么?是否返回列表?单个元素?是否再次调用?后续调用何时停止?