The Wayback Machine - https://web.archive.org/web/20160820025637/http://stackoverflow.com/posts/231767/revisions
23 Quoted "yield" for consistency with other top questions
| link

What does the yield "yield" keyword do in Python?

22 edited tags
| link
21 added 4 characters in body
source | link

What is the use of the yield keyword in Python? What does it do?

For example, I'm trying to understand this code11:

def node._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 = list(), [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? A list is returned? A single element is returned? Is it called again? When will subsequent calls stop?


1. The code comes from Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

What is the use of the yield keyword in Python? What does it do?

For example, I'm trying to understand this code1:

def node._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 = list(), [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? A list is returned? A single element is returned? Is it called again? When will subsequent calls stop?


1. The code comes from Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

What is the use of the yield keyword in Python? What does it do?

For example, I'm trying to understand this code1:

def node._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 = list(), [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? A list is returned? A single element is returned? Is it called again? When will subsequent calls stop?


1. The code comes from Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

20 fixed grammar
source | link
    Notice removed Reward existing answer by ytpillai
    Bounty Ended with e-satis's answer chosen by ytpillai
    Notice added Reward existing answer by ytpillai
    Bounty Started worth 50 reputation by ytpillai
19 added 7 characters in body
source | link
18 added 31 characters in body
source | link
17 more accurate and less preachy title
| link
16 Improved link, formatted,
source | link
    Notice removed Reward existing answer by alecxe
    Bounty Ended with e-satis's answer chosen by alecxe
    Notice added Reward existing answer by alecxe
    Bounty Started worth 500 reputation by alecxe
15 edited tags
| link
    Question Protected by wim
14 WTF???!! the OP had it right, then an hour later it was BROKEN and _stayed_ broken for FOUR YEARS ??? with all these upvotes and favs?!?? (btw: lines 705+ in source code)
source | link
13 edited tags
| link
12 minor formatting
source | link
11 deleted 6 characters in body; edited title
source | link
10 added 1 characters in body
source | link
9 tried to be more general. added link to the source code author
source | link
8 added 23 characters in body; edited title
source | link