Python的参数传递

2017-06-14  本文已影响118人  耀凯考前突击大师

Python的参数传递概述

根据Python的官方文档,Python中的参数传递机制是所谓的pass by assignment。理解这个pass by assignment需要从两个层次去考虑:

  1. 被传递的参数实际上是对象的引用(但这个引用是以值的形式传递的)。
  2. 有的对象是可变类型,有的对象是不可变类型。

那么:

下面的例子清楚地说明了上面几种情况。

传递可变类型 —— 以list为例

在方法中改变参数内容

def pass_mutable_object(input_list):
    print('input list is ', input_list)
    input_list.append(4)
    print('changed input list to ', input_list)

input_list = [1, 2, 3]

print('before the function', input_list)
pass_mutable_object(input_list)
print('after the function', input_list)

上述代码的执行结果为:

before the function [1, 2, 3]
input list is [1, 2, 3]
changed input list to [1, 2, 3, 4]
after the function [1, 2, 3, 4]

可以看出,在上面的例子中传递到pass_mutable_object中的参数是一个list,更确切地说,是该list的地址,因此我们可以在方法中对该list的内容进行修改,比如添加元素。并且这种修改操作会反应到方法外的原变量上。

在方法中改变参数的引用

下面这个例子说明了我们在方法中改变参数所对应的引用的情况。

def pass_mutable_object_modify_index(input_list):
    print('input list is', input_list)
    input_list = ['this', 'is', 'a', 'new', 'list']
    print('changed input list to', input_list)

out_list = [1, 2, 3]
print('before function', out_list)
pass_mutable_object_modify_index(out_list)
print('after function', out_list)

上面代码的执行结果如下:

before function [1, 2, 3]
input list is [1, 2, 3]
changed input list to ['this', 'is', 'a', 'new', 'list']
after function [1, 2, 3]

因为实际上传递到方法中的是out_list的引用的值的一份拷贝,所以在方法中对该变量重新赋值一个新的1ist对方法外面的out_list变量没有影响。虽然我们在方法中让input_list指向了一个新的list,但方法外的out_list没有任何变化。

传递不可变类型 —— 以String为例

String是一个不可变类型,所以我们无法在方法中修改String的值(修改不可变类型的对象的值实际上等同于重新创建了一个该类型的对象,并把新的对象的引用赋给原变量)。

下面的例子说明了向方法中传递不可变类型对象的情况。

def pass_immutable_object(input_string):
    print('input string:', input_string)
    input_string = 'We know what we are, but know not what we may be'
    print('changed to:', input_string)

out_string = 'If music be the food of love, play on'
print('before function:', out_string)
pass_immutable_object(out_string)
print('after function:', out_string)

上面程序的运行结果如下:

before function: If music be the food of love, play on
input string: If music be the food of love, play on
changed to: We know what we are, but know not what we may be
after function: If music be the food of love, play on

可以看出,和在方法中改变输入list的地址值一样,这种改变对方法外的原变量没有影响。这还是因为python中传递的参数实际上是原变量的引用的值的一份拷贝。这样的话,虽然在方法中我们使input_string变量指向了一个新的String(的地址),这种赋值对方法外的out_string没有影响。

如何实现将方法中的变化保存下来呢?

那么在很多时候,我们写一个方法是为了实现某种对参数操作的抽象。这种情况下,将方法对参数所做的改变保留下来是非常必要的。那么如何实现呢?

在Python中有两种实现方法。

使用return关键字将操作结果返回

如下面的代码所示:

def return_new_string(input_string):
    new_string = input_string + ' Quoted from William Shakespeare'
    return new_string

out_string = 'If music be the food of love, play on'
print('before function:', out_string)
out_string = return_new_string(out_string)
print('after function:', out_string)

上面代码的运行结果如下:

before function: If music be the food of love, play on
after function: If music be the food of love, play on Quoted from William Shakespeare

自定义wrapper class保存变量或使用已有的可变类型作为wrapper(如list)

如下面的代码所示:

def use_wrapper_to_reserve_changes(input):
    input[0] = input[0] + ' Quoted from William Shakespeare'

input = ['If music be the food of love, play on']
print('before function:', input[0])
use_wrapper_to_reserve_changes(input)
print('after function:', input[0])

上面代码的运行结果如下:

before function: If music be the food of love, play on
after function: If music be the food of love, play on Quoted from William Shakespeare
上一篇下一篇

猜你喜欢

热点阅读