Skip to content

Latest commit

 

History

History
133 lines (86 loc) · 5.23 KB

File metadata and controls

133 lines (86 loc) · 5.23 KB

#如何通过参数来传递一个变量?

原问题地址:http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference

##问题:

Python文档似乎没有明确指出参数是否通过引用或通过值进行传递,而且下面的代码产生了不变的值“Original”

class PassByReference:
    def __init__(self):
        self.variable = 'Original'
        self.Change(self.variable)
        print self.variable

    def Change(self, var):
        var = 'Changed'

我怎样通过实际的参数来传递变量?

##答案:

参数是通过引用来传递的(passed by assignment)。其原理是双重的:

  1. 被传入的参数实际上是对象的引用(但对象的引用是按值传递的)。
  2. 一些数据类型是可变的,但其他数据类型是不变的。

因此:

  • 如果你把一个可变对象传递到一个方法,该方法参数引用了对这个可变对象,你可以随意改变它,但如果你重新绑定该方法中的参数,外部范围将无从知晓。在你重新绑定之后,外部引用仍然指向原来的对象。
  • 如果你把一个不可变的对象传递到一个方法,你仍然无法绑定外部引用,你甚至无法改变这个对象。

为了更清楚地了解这个问题,我们来看一些例子。

###列表 - 可变类型

让我们尝试修改被传递到方法的列表:

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

输出:

before, outer_list = ['one', 'two', 'three']
got ['one', 'two', 'three']
changed to ['one', 'two', 'three', 'four']
after, outer_list = ['one', 'two', 'three', 'four']

由于被传入的参数是outer_list的一个引用,而不是它的一个副本,我们可以用改变列表的方法来改变它,让这些变化在外部范围也能体现出来。

现在让我们看看当我们试图改变通过参数传入的引用时会发生什么情况:

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

输出:

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']

由于the_list参数是按值传递的,给它分配一个新的列表丝毫不会影响到方法之外的代码。the_list是outer_list参数的一个副本,the_list指向一个新的列表,但是没有办法改变outer_list的指向。

###字符串 - 一个不可改变的类型

字符串是不可改变的,所以我们无法改变字符串的内容。

现在,让我们试着改变参数。

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

输出

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

此外,由于the_string参数是按值传递的,给它分配一个新的字符串丝毫不会影响到方法之外的代码。the_string是outer_string参数的一个副本,the_string指向一个新的字符串,但是没有办法改变outer_string的指向。

我希望上述内容对弄清这个问题有所帮助。

注:人们已经注意到,上述内容并没有回答@David最初提出的这个问题,“我怎样通过实际的参数来传递变量?”让我们着手解决这个问题。

###我们怎样解决这个问题?

正如@Andrea的回答所显示的:你可以返回到新的值。传入方式并没有改变,但你可以获取你想要取消的信息:

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)

如果你真的想避免使用返回值,你可以创建一个类来保留你的值,并把它传递到函数,或者使用一个现有的类,如列表:

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])

虽然这样做似乎有点麻烦。