「 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?
相關文章
相關標籤/搜索