Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't quite get what you're asking, but basically: when you use <code>send</code>, it causes the most-recently-reached yield expression in the generator to evaluate to the value you send. Note also that <code>send</code> advances the generator to the next <code>yield</code>. One thing that may be confusing you is that you are printing the value of <code>x</code> inside the generator, and you are printing the value of <code>next(g)</code> inside <code>b</code>, but the generator is also yielding values at <code>g.send(5)</code>, and you aren't printing those.</p> <p>In your first case, your first <code>send</code> causes the <code>yield i</code> statement to evaluate to 5 inside <code>b</code>, but you don't use this value inside <code>b</code> (you don't assign <code>yield i</code> to anything), so it does nothing. Also, when you do <code>send(5)</code>, the generator is yielding None (from the <code>x = (yield)</code> line), but you don't print it so you don't know this. Then you advance the generator again with <code>next(g)</code>. The most-recently-reached yield is the <code>x = yield</code>, but <code>next(g)</code> passes no value, so <code>x</code> gets set to None.</p> <p>In the second case, the parity of the calls is reversed. Now your first <code>send</code> does send to the <code>x = yield</code> line, so <code>x</code> gets set to 5. This <code>send</code> also yields the loop value in <code>b</code>, but you ignore this value in <code>a</code> and don't print it. You then print <code>next(g)</code>, which is None. On each subsequent send, <code>b</code> prints the value of <code>x</code>, which is always 5 because that's what you always send, and then <code>a</code> prints the <em>next</em> yielded value, which is always None (because that's what <code>x = yield</code> yields).</p> <p>I don't quite get what you mean by "using the yield statement to both receive and send values in the same function". You can certainly do this, but you have to realize that: a) a value (None) is still sent even when you call <code>next(g)</code>; and b) a value is still yielded when you call <code>g.send(5)</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload