Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simply put, <code>yield</code> gives you a generator. You'd use it where you would normally use a <code>return</code> in a function. As a really contrived example cut and pasted from a prompt...</p> <pre><code>&gt;&gt;&gt; def get_odd_numbers(i): ... return range(1, i, 2) ... &gt;&gt;&gt; def yield_odd_numbers(i): ... for x in range(1, i, 2): ... yield x ... &gt;&gt;&gt; foo = get_odd_numbers(10) &gt;&gt;&gt; bar = yield_odd_numbers(10) &gt;&gt;&gt; foo [1, 3, 5, 7, 9] &gt;&gt;&gt; bar &lt;generator object yield_odd_numbers at 0x1029c6f50&gt; &gt;&gt;&gt; bar.next() 1 &gt;&gt;&gt; bar.next() 3 &gt;&gt;&gt; bar.next() 5 </code></pre> <p>As you can see, in the first case <code>foo</code> holds the entire list in memory at once. It's not a big deal for a list with 5 elements, but what if you want a list of 5 million? Not only is this a huge memory eater, it also costs a lot of time to build at the time that the function is called. In the second case, <code>bar</code> just gives you a generator. A generator is an iterable--which means you can use it in a for loop, etc, but each value can only be accessed once. All the values are also not stored in memory at the same time; the generator object "remembers" where it was in the looping the last time you called it--this way, if you're using an iterable to (say) count to 50 billion, you don't have to count to 50 billion all at once and store the 50 billion numbers to count through. Again, this is a pretty contrived example, you probably would use <code>itertools</code> if you really wanted to count to 50 billion. :)</p> <p>This is the most simple use case of generators. As you said, it can be used to write efficient permutations, using <code>yield</code> to push things up through the call stack instead of using some sort of stack variable. Generators can also be used for specialized tree traversal, and all manner of other things.</p> <p>Further reading:</p> <ul> <li>python wiki <a href="http://wiki.python.org/moin/Generators" rel="noreferrer">http://wiki.python.org/moin/Generators</a></li> <li>PEP on generators <a href="http://www.python.org/dev/peps/pep-0255/" rel="noreferrer">http://www.python.org/dev/peps/pep-0255/</a></li> </ul>
    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. 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