+5 votes
2 views
in Python by (1.5k points)

Can anyone tell me the use of yield keyword in python and what actually it does?

Here is me code what actually I am trying

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 
 

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 _get_child_candidates method will be called? The list will be returned or not? A single element? Can it be called again and when subsequent calls stop?

2 Answers

+3 votes
by (10.9k points)
edited by
Python generators mainly use Yield. A generator function is defined like a normal function, whenever it needs to generate a value, it is done by using the yield keyword rather than return. When yield keyword is in the body of a def, the function automatically becomes a generator function.

With reference to the code, candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) exhausts all values of the generator and while keeps on generating new generator objects which produce different values every time as it is not applied on the same node. The function gets_child_candidates acts as an iterator and it adds one element at a time to the new list when you extend the list.

Also, in your code, a generator is passed as an argument to extend() method even though it is a list object method. Since Python expects iterables so it works with everything even with generators.
+1 vote
by (107k points)
edited by
  • The yield statement stops the function’s execution and sends a call back to the caller but keeps enough space to resume function. After the function is resumed it allows its code to produce a series of values over time rather than computing them at once and sending them back to the list.
  • Yield keyword replaces the return of a function to provide a result to its caller without affecting local variables
  • An example that demonstrates the above definition:-

def GeneratorFun():

    yield 1

    yield 2

    yield 3

#Caller or Driver code to check above function.

for value in GeneratorFun(): 

    print(value)

  • Generators are defined using the yield keyword. 
  • Now see what happens when you call _get_child_candidates() method in your code:-
  • candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) in your code exhausts all the values of the generator but keeps creating new generator objects which will produce different values from the previous ones since it is not applied on the same node.
  • You have used the extend() method which is a list object method that expects an iterable and adds its values to the list.
  • Usually, we pass a list to it:

x = [1, 2] 

y= [3, 4]

x.extend(y)

print(x) 

  • In your code instead of a list it gets a generator, which is good:
  1. So here you do not need to read the values twice.
  2. You can have many things in your code and you do not want all of them stored in a memory.
  • It works only because Python does not care about the argument of a method whether it is a list or not a list. It only expects iterables so that, it will work with strings, lists, tuples, and generators.

You can use the following video tutorials to clear all your doubts:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...