What is the reason python handles locals() this way (in pairs)?
<p>Get this simple python code, same matching with re.compile instance.
I noticed that even though I am using the very same value, it creates two instances, and repeats them accordingly.</p>

<p>I wonder if one can tell the reason for this behavior, </p>

<ul>
<li>Why does it create the second instance at all?    </li>
<li>Why only two?   </li>
<li>And why each time picked the other one and not randomly?</li>
</ul>

<p><strong>the CLI code:</strong></p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt;
&gt;&gt;&gt; rec = re.compile("(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)")
&gt;&gt;&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb238&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb238&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb238&gt;
&gt;&gt;&gt; rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
</code></pre>

<p><strong>Edit:</strong></p>

<p>As <a href="http://stackoverflow.com/users/180174/kimvais">@kimvais</a> <a href="http://stackoverflow.com/a/9160431/9296">answered</a>, the reason lays in the <code>_</code> which holds the latest assignment.
see, if you not assigning, rather printing, it is the same one, all the time.</p>

<pre><code>&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
&gt;&gt;&gt; print rec.match('www.example.com')
&lt;_sre.SRE_Match object at 0x23cb1d0&gt;
</code></pre>
