擅长:python、mysql、java
<p>Python可以重用内存位置。在</p>
<p>跑步时:</p>
<pre><code>id(1.1)
</code></pre>
<p>创建一个float值,请求它的<code>id()</code>,然后Python<em>再次删除该值,因为没有引用它。然后,当您创建另一个<em>浮点值时,Python可以重用相同的内存位置,因此<code>id(2.2)</code>很可能为<code>id()</code>返回相同的值:</p>
^{pr2}$
<p>请执行以下操作:</p>
<pre><code>float_one, float_two = 1.1, 2.2
print id(float_one), id(float_two)
</code></pre>
<p>现在浮点值引用了它们(这两个变量),不会被销毁,它们现在有不同的内存位置,因此<code>id()</code>值。在</p>
<p>对于小整数(从-5到256),您看到不同的<code>id()</code>值是因为<a href="https://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers">these values are interned</a>;Python只创建<em>一个</em><code>1</code>整数对象,并反复使用它。因此,这些整数都有一个唯一的内存地址regardles,因为Python解释器本身已经引用了它们,并且在解释器退出之前不会删除它们。在</p>