Skip to main content
Add documentation link.
Source Link
Mateen Ulhaq
  • 26.6k
  • 20
  • 115
  • 147

What functionality does the 'yield'yield keyword in Python provide?

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

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  

And this is the caller:

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 method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?

  
1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

What functionality does the 'yield' keyword in Python provide?

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

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  

And this is the caller:

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 method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?

 
1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

What functionality does the yield keyword in Python provide?

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

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  

And this is the caller:

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 method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?

 
1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.
Condense first two sentences into one single, clearer sentence.
Source Link

What is the use offunctionality does the yield'yield' keyword in Python? What does it do provide?

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

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  

And this is the caller:

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 method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?


1. This piece of code was written by 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 _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 = [], [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? Is a list returned? A single element? Is it called again? When will subsequent calls stop?


1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.

What functionality does the 'yield' keyword in Python provide?

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

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  

And this is the caller:

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 method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?


1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source: Module mspace.
added programming language reference for better clarity and future references
Link
Loading
Rollback to Revision 41
Link
Martijn Pieters
  • 1.1m
  • 313
  • 4.2k
  • 3.4k
Loading
Rollback to Revision 45
Link
Martijn Pieters
  • 1.1m
  • 313
  • 4.2k
  • 3.4k
Loading
edited tags
Link
Loading
A question with version-specific python-2.7 and python-3.x tags is pragmatically just "python" to 99.9% of users.
Link
Mateen Ulhaq
  • 26.6k
  • 20
  • 115
  • 147
Loading
edited tags
Link
oguz ismail
  • 49.2k
  • 16
  • 53
  • 75
Loading
edited tags
Link
Loading
edited tags; edited title
Link
Loading
edited tags
Link
Mateen Ulhaq
  • 26.6k
  • 20
  • 115
  • 147
Loading
edited tags
Link
Braiam
  • 4.5k
  • 11
  • 49
  • 80
Loading
Corrected the link, as the format did not work in <sub>
Source Link
Loading
Rollback to Revision 36
Source Link
Buddy Bob
  • 5.9k
  • 1
  • 16
  • 48
Loading
Notice added Needs detailed answers by Martijn Pieters
added 4 characters in body
Source Link
John Smith
  • 7.4k
  • 6
  • 51
  • 63
Loading
Notice removed Reward existing answer by U13-Forward
Bounty Ended with Bite code's answer chosen by U13-Forward
Notice added Reward existing answer by U13-Forward
Bounty Started worth 50 reputation by U13-Forward
added 14 characters in body
Source Link
Alec
  • 9.2k
  • 8
  • 40
  • 66
Loading
deleted 4 characters in body
Source Link
linusg
  • 6.4k
  • 4
  • 31
  • 80
Loading
Edited question title and removed language tag.
Link
martineau
  • 122.4k
  • 29
  • 176
  • 307
Loading
Rollback to Revision 31
Source Link
Taryn
  • 246.1k
  • 57
  • 368
  • 407
Loading