相关疑难解决方法(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万
查看次数

Python中yield表达式的结果是什么?

我知道yield将函数转换为生成器,但yield表达式本身的返回值是多少?例如:

def whizbang(): 
    for i in range(10): 
        x = yield i
Run Code Online (Sandbox Code Playgroud)

x这个函数执行时变量的值是多少?

我已经阅读了Python文档:http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt,似乎没有提到yield表达式本身的值.

python yield generator

55
推荐指数
1
解决办法
2万
查看次数

标签 统计

generator ×2

python ×2

yield ×2

coroutine ×1

iterator ×1