Search code examples
pythongeneratorsend

What's different from these two functions using send in python


I am learning generator in python,here the functions:

import math
def is_primes(number):
    if number > 1:
        if number == 2:
            return True
        if number % 2 == 0:
            return False
        for current in range(3, int(math.sqrt(number) + 1), 2):
            if number % current == 0:
                return False
        return True
    return False


def get_primes1(number):
    while True:
        if is_primes(number):
            yield number
        number +=1

def get_primes2(number):
    while True:
        if is_primes(number):
            number=yield number
        number +=1

when using the send function:

a=get_primes1(2)
b=get_primes2(2)
a.send(None)#return 2
b.send(None)#return 2
a.send(1)#return 3,the parameter in send() looks useless.
b.send(1)#return 1,normal

when using the "a.send(1)" and "b.send(1)" repeatly,a.send(1) return the bigger value but b.send(1) still return the same value.why there is difference?

i check the meaning of send function:

Resumes the execution and ``sends'' a value into the generator function. The value argument becomes the result of the current yieldexpression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receieve the value.


Solution

  • how the send function work in generator?

    def counter():
        total = 0
    
        while True:
            increment = yield total  #If next() was called, 
                                     #increment is assigned None. 
                                     #If send() was called,
                                     #increment is assigned send()'s argument.
    
            if increment:
                total += increment
            else:
                total += 1
    
    
    g = counter()
    print(next(g))
    print(next(g))
    print(g.send(3))
    print(next(g))
    
    --output:--
    0
    1
    4
    5
    

    A yield expression's value is None whenever the generator is resumed by a normal next() call.


    send() method for generator-iterators, which resumes the generator and "sends" a value that becomes the result of the current yield-expression. The send() method returns the next value yielded by the generator

    See PEP 342

    but b.send(1) still return the same value.why there is difference?

    Because you are resetting number to the value of send()'s argument over and over again:

    a:  number = 2
        number += 1
        number += 1
    
    b:  number = 2
        number = 1
        number += 1
        number = 1
        number += 1