What is the use of keyword yield
in Python? What are you doing?
I am trying to understand the following 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
The function call (or method in this case) is:
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 _get_child_candidates method is called? Does it return a list or a single element? Is it called again? When will subsequent calls stop?
1. The code comes from Jochen Schulz (jrschulz), who created an excellent Python library for metric spaces. This is the link to the full source: Module mspace .