Users Pricing

forum

Home Forums What does the yield keyword do in Python? – MindStick

What does the yield keyword do in Python?

Anonymous User 3115 11 May 2015
What is the use of the yield keyword in Python? What does it do?

For example, I'm trying to understand this code (**):

def node._get_child_candidates(self, distance, min_dist, madist):
    if self._leftchild and distance - madist < self._median:
        yield self._leftchild
    if self._rightchild and distance + madist >= self._median:
        yield self._rightchild  
And this is the caller:

result, candidates = list(), [self]
while candidates:
    node = candidates.pop()
    distance = node._get_dist(obj)
    if distance <= madist and distance >= min_dist:
        result.extend(node._values)
    candidates.extend(node._get_child_candidates(distance, min_dist, madist))
return result
What happens when the method _get_child_candidates is called? A list is returned? A single element is returned? Is it called again? When subsequent calls do stop?

2 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md