开发者社区 问答 正文

如何通过引用传递变量?

Python文档似乎不清楚参数是通过引用还是值传递的,下面的代码生成未更改的值“Original”

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

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

是否可以通过实际引用来传递变量? 问题来源StackOverflow 地址:/questions/59382244/does-list-passed-by-reference-in-python

展开
收起
kun坤 2019-12-27 17:15:06 533 分享 版权
1 条回答
写回答
取消 提交回答
  • 参数通过赋值传递。这背后的理由有两方面: 这样的: 为了让它更清楚,我们来看一些例子。 让我们尝试修改传递给一个方法的列表:

    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的引用,而不是它的副本,所以我们可以使用mutating 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])
    

    尽管这看起来有点麻烦。

    2019-12-27 17:15:14
    赞同 展开评论
问答分类:
问答地址: