34

I'm familiar with yield to return a value thanks mostly to this question

but what does yield do when it is on the right side of an assignment?

@coroutine
def protocol(target=None):
   while True:
       c = (yield)

def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr 
    return start

I came across this, on the code samples of this blog, while researching state machines and coroutines.

Community
  • 1
  • 1
Fire Crow
  • 7,499
  • 4
  • 36
  • 35

3 Answers3

40

The yield statement used in a function turns that function into a "generator" (a function that creates an iterator). The resulting iterator is normally resumed by calling next(). However it is possible to send values to the function by calling the method send() instead of next() to resume it:

cr.send(1)

In your example this would assign the value 1 to c each time.

cr.next() is effectively equivalent to cr.send(None)

Tendayi Mawushe
  • 25,562
  • 6
  • 51
  • 57
  • 1
    Just to note, before being able to call `send()` on a generator, you have to call `next()` to actually start it, or you'll get a TypeError saying: `TypeError: can't send non-None value to a just-started generator` – Caumons May 26 '15 at 14:06
12

You can send values to the generator using the send function.

If you execute:

p = protocol()
p.next() # advance to the yield statement, otherwise I can't call send
p.send(5)

then yield will return 5, so inside the generator c will be 5.

Also, if you call p.next(), yield will return None.

You can find more information here.

dusan
  • 9,104
  • 3
  • 35
  • 55
0
  • yield returns a stream of data as per the logic defined within the generator function.
  • However, send(val) is a way to pass a desired value from outside the generator function.

p.next() doesn't work in python3, next(p) works (built-in) for both python 2,3

p.next() doesn't work with python 3, gives the following error,however it still works in python 2.

Error: 'generator' object has no attribute 'next'

Here's a demonstration:

def fun(li):
  if len(li):
    val = yield len(li)
    print(val)
    yield None
    

g = fun([1,2,3,4,5,6])
next(g) # len(li) i.e. 6 is assigned to val
g.send(8) #  8 is assigned to val

Community
  • 1
  • 1
Aditya Patnaik
  • 1,490
  • 17
  • 27