相关疑难解决方法(0)

"yield"关键字有什么作用?

yieldPython中关键字的用途是什么?它有什么作用?

例如,我试图理解这段代码1:

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  
Run Code Online (Sandbox Code Playgroud)

这是来电者:

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
Run Code Online (Sandbox Code Playgroud)

_get_child_candidates调用该方法时会发生什么?列表是否返回?单个元素?它又被召唤了吗?后续通话何时停止?


1.代码来自Jochen Schulz(jrschulz),他为度量空间创建了一个很棒的Python库.这是完整源代码的链接:模块mspace.

python iterator yield generator coroutine

9664
推荐指数
46
解决办法
212万
查看次数

文件中的最长行

我正在寻找一种简单的方法来查找文件中最长行的长度.理想情况下,它将是一个简单的bash shell命令而不是脚本.

bash shell utilities

187
推荐指数
8
解决办法
9万
查看次数

标签 统计

bash ×1

coroutine ×1

generator ×1

iterator ×1

python ×1

shell ×1

utilities ×1

yield ×1