What does the "yield" keyword do?
-
What is the use of the yield keyword in the Python framework? What does it do? [EDIT: Link removed --JKSH]
For example, I'm trying to understand this code
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_dist and distance >= min_dist: result.extend(node._values) candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) return result
What happens when the method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?
-
@Aliviya said in What does the "yield" keyword do?:
yield
returns a gerator object which can be iterated to get the values.
See https://www.guru99.com/python-yield-return-generator.html -
@jsulm said in What does the "yield" keyword do?:
returns a gerator object which can be iterated to get the values.
See https://www.guru99.com/python-yield-return-generator.htmlThank you..It has a great overview of how yield works and how to use it.