python引用传递_Python中如何通过引用传递变量?

小编典典

传入的参数实际上是对对象的引用(但引用是通过值传递的)

有些数据类型是可变的,但有些则不是

所以:

如果将可变对象传递给方法,则该方法将获得对该对象的引用,并且可以对其进行突变,但是如果您将该引用重新绑定到该方法中,则外部作用域对此一无所知完成后,外部参考仍将指向原始对象。

如果将不可变对象传递给方法,则仍然无法重新绑定外部引用,甚至无法使对象发生突变。

为了更加清楚,让我们举一些例子。

列表-可变类型

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

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

尽管这看起来有点麻烦。

2020-02-03

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值