擅长:python、mysql、java
<p>Python中没有指针。有一些东西称为引用(它和C++引用一样,通常在指针中实现),但是不同于C++引用并不意味着通过引用。每个变量都存储对分配到其他位置(堆上)的对象的引用。每个集合存储对分配到其他地方的对象的引用。对象的每个成员都存储对分配到其他位置的对象的引用。在</p>
<p>简单表达式<code>x</code>的计算结果是存储在<code>x</code>中的引用,无论使用它的人都无法确定它来自变量。无法获得一个链接到一个<em>变量</em>(与它的内容相反)的链接,该链接可用于跟踪该变量的变化。项(<code>x[y] = ...</code>)和成员(<code>x.y = ...</code>)分配在一个方面是不同的:它们调用方法并对现有对象进行变异,而不是重写局部变量。在处理作用域时,这种差异主要很重要,但是您可以使用其中任何一个来模拟不可变类型的可变性(如@Greg Hewgill所示),并跨函数边界共享状态更改(<code>def f(x): x = 0</code>不会更改任何内容,但是<code>def g(x): x.x = 0</code>可以)。不过,这并不完全取决于模拟按引用传递-除非用包装器对象替换每个变量,该对象的唯一目的是保存可变的<code>val</code>属性。这相当于在C中通过指针模拟按引用传递,但要麻烦得多。在</p>