yield

What does the “yield” keyword do?

佐手、 提交于 2019-11-25 23:55:21
问题 What is the use of the yield keyword in Python? What does it do? For example, I\'m trying to understand this code 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 And this is the caller: result, candidates = [], [self] while candidates: node = candidates.pop() distance = node._get_dist(obj) if distance <= max

What is Scala&#39;s yield?

落爺英雄遲暮 提交于 2019-11-25 23:28:48
问题 I understand Ruby and Python\'s yield. What does Scala\'s yield do? 回答1: It is used in sequence comprehensions (like Python's list-comprehensions and generators, where you may use yield too). It is applied in combination with for and writes a new element into the resulting sequence. Simple example (from scala-lang) /** Turn command line arguments to uppercase */ object Main { def main(args: Array[String]) { val res = for (a <- args) yield a.toUpperCase println("Arguments: " + res.toString) }