<p>参数是<a href="http://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference" rel="noreferrer">passed by assignment</a>。这背后的理由有两个:</p>
<ol>
<li>传入的参数实际上是对象的<em>引用</em>(但引用是按值传递的)</li>
<li>有些数据类型是可变的,但有些则不是</li>
</ol>
<p>所以:</p>
<ul>
<li><p>如果你把一个可变的对象传给一个方法,这个方法会得到一个对同一个对象的引用,你可以让它发生变化,让你高兴,但是如果你在这个方法中重新绑定引用,外部作用域对它一无所知,完成后,外部引用仍然指向原始对象。</p></li>
<li><p>如果将不可变的对象传递给方法,则仍然无法重新绑定外部引用,甚至无法对该对象进行变异。</p></li>
</ul>
<p>为了更清楚地说明这一点,让我们举几个例子。</p>
<h2>List-可变类型</h2>
<p><strong>让我们尝试修改传递给方法的列表:</strong></p>
<pre><code>def try_to_change_list_contents(the_list):
print('got', the_list)
the_list.append('four')
print('changed to', the_list)
outer_list = ['one', 'two', 'three']
print('before, outer_list =', outer_list)
try_to_change_list_contents(outer_list)
print('after, outer_list =', outer_list)
</code></pre>
<p>输出:</p>
<pre class="lang-none prettyprint-override"><code>before, outer_list = ['one', 'two', 'three']
got ['one', 'two', 'three']
changed to ['one', 'two', 'three', 'four']
after, outer_list = ['one', 'two', 'three', 'four']
</code></pre>
<p>因为传入的参数是对<code>outer_list</code>的引用,而不是它的副本,所以我们可以使用变异列表方法来更改它,并将更改反映在外部作用域中。</p>
<p><strong>现在让我们看看当我们试图更改作为参数传入的引用时会发生什么:</strong></p>
<pre><code>def try_to_change_list_reference(the_list):
print('got', the_list)
the_list = ['and', 'we', 'can', 'not', 'lie']
print('set to', the_list)
outer_list = ['we', 'like', 'proper', 'English']
print('before, outer_list =', outer_list)
try_to_change_list_reference(outer_list)
print('after, outer_list =', outer_list)
</code></pre>
<p>输出:</p>
<pre class="lang-none prettyprint-override"><code>before, outer_list = ['we', 'like', 'proper', 'English']
got ['we', 'like', 'proper', 'English']
set to ['and', 'we', 'can', 'not', 'lie']
after, outer_list = ['we', 'like', 'proper', 'English']
</code></pre>
<p>由于<code>the_list</code>参数是按值传递的,因此给它分配一个新列表并没有方法外部的代码可以看到的效果。<code>the_list</code>是<code>outer_list</code>引用的副本,我们有一个指向新列表的<code>the_list</code>点,但是无法更改<code>outer_list</code>点的位置。</p>
<h2>String-不可变类型</h2>
<p><strong>它是不可变的,因此我们无法更改字符串的内容</strong></p>
<p><strong>现在,让我们尝试更改引用</strong></p>
<pre><code>def try_to_change_string_reference(the_string):
print('got', the_string)
the_string = 'In a kingdom by the sea'
print('set to', the_string)
outer_string = 'It was many and many a year ago'
print('before, outer_string =', outer_string)
try_to_change_string_reference(outer_string)
print('after, outer_string =', outer_string)
</code></pre>
<p>输出:</p>
<pre class="lang-none prettyprint-override"><code>before, outer_string = It was many and many a year ago
got It was many and many a year ago
set to In a kingdom by the sea
after, outer_string = It was many and many a year ago
</code></pre>
<p>同样,由于<code>the_string</code>参数是按值传递的,因此给它分配一个新字符串没有方法外部的代码可以看到的效果。<code>the_string</code>是<code>outer_string</code>引用的副本,我们有一个指向新字符串的<code>the_string</code>点,但是无法更改<code>outer_string</code>点的位置。</p>
<p>我希望这能澄清一些事情。</p>
<p><strong>编辑:</strong>我们注意到,这并不能回答@David最初提出的问题:“有什么我可以做的,通过实际引用传递变量吗?”。让我们继续努力吧。</p>
<h2>我们该怎么解决这个问题?</h2>
<p>正如@Andrea的答案所示,您可以返回新值。这不会改变传递信息的方式,但会让您获得想要的信息:</p>
<pre><code>def return_a_whole_new_string(the_string):
new_string = something_to_do_with_the_old_string(the_string)
return new_string
# then you could call it like
my_string = return_a_whole_new_string(my_string)
</code></pre>
<p>如果您真的想避免使用返回值,可以创建一个类来保存您的值并将其传递到函数中,或者使用现有的类,如列表:</p>
<pre><code>def use_a_wrapper_to_simulate_pass_by_reference(stuff_to_change):
new_string = something_to_do_with_the_old_string(stuff_to_change[0])
stuff_to_change[0] = new_string
# then you could call it like
wrapper = [my_string]
use_a_wrapper_to_simulate_pass_by_reference(wrapper)
do_something_with(wrapper[0])
</code></pre>
<p>虽然这看起来有点麻烦。</p>