Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Mark Tolonen pointed out in a concise comment, the reason why your itertools attempts are cycling indefinitely is because, for the list assignment, python is checking the length of the right hand side.</p> <p>Now to really dig in...</p> <p>When you say:</p> <pre><code>x[::2] = itertools.repeat(False) </code></pre> <p>The left hand side (<code>x[::2]</code>) is a list, and you are assigning a value to a list where the value is the <code>itertools.repeat(False)</code> iterable, which will iterate forever since it wasn't given a length (as per <a href="http://docs.python.org/library/itertools.html#itertools.repeat" rel="nofollow">the docs</a>).</p> <p>If you dig into the list assignment code in the cPython implementation, you'll find the unfortunately/painfully named function <a href="http://hg.python.org/cpython/file/8527427914a2/Objects/listobject.c#l614" rel="nofollow"><code>list_ass_slice</code></a>, which is at the root of a lot of list assignment stuff. In that code you'll see <a href="http://hg.python.org/cpython/file/8527427914a2/Objects/listobject.c#l646" rel="nofollow">this segment</a>:</p> <pre><code>v_as_SF = PySequence_Fast(v, "can only assign an iterable"); if(v_as_SF == NULL) goto Error; n = PySequence_Fast_GET_SIZE(v_as_SF); </code></pre> <p>Here it is trying to get the length (<code>n</code>) of the iterable you are assigning to the list. However, before even getting there it is getting stuck on <a href="http://hg.python.org/cpython/file/8527427914a2/Objects/abstract.c#l2257" rel="nofollow"><code>PySequence_Fast</code></a>, where it ends up trying to convert your iterable to a list (with <a href="http://hg.python.org/cpython/file/8527427914a2/Objects/abstract.c#l2235" rel="nofollow"><code>PySequence_List</code></a>), within which it ultimately creates an empty list and tries to simply extend it with your iterable.</p> <p>To extend the list with the iterable, it uses <a href="http://hg.python.org/cpython/file/8527427914a2/Objects/listobject.c#l799" rel="nofollow"><code>listextend()</code></a>, and in there you'll see the root of the problem:</p> <pre><code>/* Run iterator to exhaustion. */ for (;;) { </code></pre> <p>and there you go.</p> <p>Or least I think so... :) It was an interesting question so I thought I'd have some fun and dig through the source to see what was up, and ended up there.</p> <p>As to the different behaviour with numpy arrays, it will simply be a difference in how the <code>numpy.array</code> assignments are handled.</p> <p>Note that using <code>itertools.repeat</code> doesn't work in numpy, but it doesn't hang up (I didn't check the implementation to figure out why):</p> <pre><code>&gt;&gt;&gt; import numpy, itertools &gt;&gt;&gt; x = numpy.ones(10,dtype='bool') &gt;&gt;&gt; x[::2] = itertools.repeat(False) &gt;&gt;&gt; x array([ True, True, True, True, True, True, True, True, True, True], dtype=bool) &gt;&gt;&gt; #but the scalar assignment does work as advertised... &gt;&gt;&gt; x = numpy.ones(10,dtype='bool') &gt;&gt;&gt; x[::2] = False &gt;&gt;&gt; x array([False, True, False, True, False, True, False, True, False, True], dtype=bool) </code></pre>
 

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