forum

Home / DeveloperSection / Forums / What does the yield keyword do in Python?

What does the yield keyword do in Python?

Anonymous User 1881 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?

Updated on 19-Jun-2023
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By