title: Python: set(sympy.primerange(a, b))
body: <p>(Python 3.5.1)</p>
  
  <p>I've been trying to use Sympy for some Project Euler problems, but I've come across something weird about how <code>set(sympy.primerange(a, b))</code> and similar constructions work.</p>
  
  <pre><code>&gt;&gt;&gt; import sympy
  &gt;&gt;&gt; PR = sympy.primerange(1, 20)
  &gt;&gt;&gt; set(PR)
  {2, 3, 5, 7, 11, 13, 17, 19}
  </code></pre>
  
  <p>So far, so good. But:</p>
  
  <pre><code>&gt;&gt;&gt; import sympy
  &gt;&gt;&gt; PR = sympy.primerange(1, 20)
  &gt;&gt;&gt; set(PR)
  {2, 3, 5, 7, 11, 13, 17, 19}
  &gt;&gt;&gt; set(PR)
  set()
  </code></pre>
  
  <p>Calling simply <code>PR</code> gives me <code>&lt;generator object primerange at 0x039C1720&gt;</code> after calling <code>list(PR)</code> once <em>or</em> twice. The same thing happens with <code>for p in PR: print(p)</code> and <code>list(PR)</code>.</p>
  
  <p>Why does this not work:</p>
  
  <pre><code>&gt;&gt;&gt; import sympy, itertools
  &gt;&gt;&gt; sympy.sieve.extend(100)
  &gt;&gt;&gt; set(itertools.takewhile(lambda p: p&lt;20, sympy.sieve))
  set()
  &gt;&gt;&gt; sympy.sieve
  &lt;Sieve with 25 primes sieved: 2, 3, 5, ... 89, 97&gt;
  </code></pre>
  
  <p>Why don't we get the set <code>{2, 3, 5, 7, 11, 13, 17, 19}</code>?</p>