<pre><code>>>> id(1.1)
154154684
</code></pre>
<p>由于1.1未分配给任何变量,因此它被垃圾回收,下次将使用相同的id作为浮点:</p>
^{pr2}$
<p>将1.1保存在变量中:</p>
<pre><code>>>> f = 1.1
>>> id(f)
154154684 #this id is locked for now
</code></pre>
<p>现在使用新地址:</p>
<pre><code>>>> id(1.1)
154154700
>>> id(2.2)
154154700
</code></pre>
<p>这同样适用于<strong>整数</strong>:</p>
<pre><code>>>> id(260)
154302180
>>> id(280)
154302180
</code></pre>
<p>从<code>-5</code>到{<cd2>}的整数实际上缓存在python中,因此它们总是返回不同的id。(<a href="https://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers">"is" operator behaves unexpectedly with integers</a>)</p>
<p>对于<strong>字符串</strong>:</p>
<p>与整数一样,一些字符串也缓存在python中。因此,这类字符串的<code>id</code>将不同(有关详细信息,请阅读:<a href="https://stackoverflow.com/questions/16756699/is-operator-behaves-differently-when-comparing-strings-with-spaces">'is' operator behaves differently when comparing strings with spaces</a>):</p>
<pre><code>>>> id('foo')
162861592
>>> id('foo')
162861568
</code></pre>
<p>非字母数字字符串(使用相同的<code>id</code>):</p>
<pre><code>>>> id('foo!&9((&')
162840000
>>> id('foo!&9((&')
162840000
</code></pre>