<span class="n">a</span> <span class="o">=</span> <span class="s">"abc"</span> <span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="n">a</span> <span class="o">=</span> <span class="s">"XYZ"</span> <span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<code class="language-python3"><span class="n">In</span> <span class="p">[</span><span class="mi">8</span><span class="p">]:</span> <span class="n">string_1</span> <span class="o">=</span> <span class="s">"EOF"</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">9</span><span class="p">]:</span> <span class="n">string_2</span> <span class="o">=</span> <span class="s">"EOF"</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">11</span><span class="p">]:</span> <span class="nb">print</span> <span class="s">"address 1: %x"</span> <span class="o">%</span> <span class="nb">id</span><span class="p">(</span><span class="n">string_1</span><span class="p">)</span>
<span class="n">address</span> <span class="mi">1</span><span class="p">:</span> <span class="mi">7</span><span class="n">f2517898530</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">12</span><span class="p">]:</span> <span class="nb">print</span> <span class="s">"address 2: %x"</span> <span class="o">%</span> <span class="nb">id</span><span class="p">(</span><span class="n">string_2</span><span class="p">)</span>
<span class="n">address</span> <span class="mi">2</span><span class="p">:</span> <span class="mi">7</span><span class="n">f2517898530</span>
</code>
请不要使用Java那一套思想来理解python。
编译器不懂。不发表评论。<code class="language-text">a = "str"
b = "str"
a is b
</code>
记住一点:CPython为例,所有变量都是一个PyObject*,也就是指针,而所有赋值语句都是将一个对象的地址赋值给它,其实不光是变量,容器里面的也是,比如list和dict的元素等,所以你这里的代码就相当于: