Users Pricing

forum

home / developersection / forums / what does the yield keyword do in python?

What does the yield keyword do in Python?

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

I am a content writter !


2 Answers