Python知識(shí)分享網(wǎng) - 專業(yè)的Python學(xué)習(xí)網(wǎng)站 學(xué)Python,上Python222
【面試必備】全網(wǎng)最火的100道 Python 面試題!PDF 下載
發(fā)布于:2023-08-12 10:30:43
(假如點(diǎn)擊沒反應(yīng),多刷新兩次就OK!)

【面試必備】全網(wǎng)最火的100道 Python 面試題!PDF 下載  圖1

 

 

資料內(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