Tag: 协程

“yield”关键字有什么作用?

Python中yield关键字的用法是什么? 它有什么作用? 例如,我试图理解这个代码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 这是来电者: 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)) […]