网友问题:
参数是通过引用还是按值传递的?如何通过引用传递,以便输出下面的代码而不是?'Changed'
'Original'
class PassByReference:
def __init__(self):
self.variable = 'Original'
self.change(self.variable)
print(self.variable)
def change(self, var):
var = 'Changed'
另请参阅:为什么函数可以修改调用方感知到的某些参数,而不能修改其他参数?
网友回答:
问题来自对Python中变量的误解。如果你习惯了大多数传统语言,那么你有一个心理模型,可以按照以下顺序发生什么:
a = 1
a = 2
你认为这是一个存储值的内存位置,然后更新以存储该值。这不是Python中的工作方式。相反,开始作为对具有值的对象的引用,然后被重新分配为对具有值的对象的引用。这两个对象可能会继续共存,即使不再引用第一个对象;事实上,它们可以由程序中任何其他参考共享。a
1
2
a
1
2
a
使用参数调用函数时,将创建一个引用传入对象的新引用。这与函数调用中使用的引用是分开的,因此无法更新该引用并使其引用新对象。在您的示例中:
def __init__(self):
self.variable = 'Original'
self.Change(self.variable)
def Change(self, var):
var = 'Changed'
self.variable
是对字符串对象的引用。调用时,将创建对该对象的第二个引用。在函数内部,您将引用重新分配给不同的字符串对象,但引用是独立的,不会更改。'Original'
Change
var
var
'Changed'
self.variable
解决此问题的唯一方法是传递可变对象。由于两个引用都引用同一对象,因此对对象所做的任何更改都会反映在两个位置。
def __init__(self):
self.variable = ['Original']
self.Change(self.variable)
def Change(self, var):
var[0] = 'Changed'
网友回答:
我发现其他答案相当冗长和复杂,所以我创建了这个简单的图表来解释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])
虽然这似乎有点麻烦。
模板简介:该模板名称为【如何通过引用传递变量?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Python】栏目查找您需要的精美模板。