資料內(nèi)容:
Python中關(guān)鍵字yield有什么作用?
yield有什么用?
例如下面這段代碼:
def node._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
下面是調(diào)用它:
result, candidates = list(), [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
當(dāng) _get_child_candidates 方法被調(diào)用的時(shí)候發(fā)生了什么?是返回一個(gè)列表?還是一個(gè)元祖?它
還能第二次調(diào)用嗎?后面的調(diào)用什么時(shí)候結(jié)束?
為了理解yield有什么用,首先得理解generators,而理解generators前還要理解iterables