Python知识分享网 - 专业的Python学习网站 学Python,上Python222
【面试必备】全网最火的100道 Python 面试题!PDF 下载
发布于:2023-08-12 10:30:43
(假如点击没反应,多刷新两次就OK!)

【面试必备】全网最火的100道 Python 面试题!PDF 下载  图1

 

 

资料内容:

 

 

Python中关键字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
下面是调用它:
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
_get_child_candidates 方法被调用的时候发生了什么?是返回一个列表?还是一个元祖?
还能第二次调用吗?后面的调用什么时候结束?
为了理解yield有什么用,首先得理解generators,而理解generators前还要理解iterables