Python的函数参数传递:传值?引用?
2017-10-31 19:10
337 查看
可更改(mutable)与不可更改(immutable)对象
对应于上一个概念,就必须引出另了另一概念,这就是可更改(mutable)对象与
不可更改(immutable)对象。
对于python比较熟悉的人们都应该了解这个事实,在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象。那么,这些所谓的可改变和不可改变影响着什么呢?
可更改vs不可更改
1 2 3 4 5 | nfoo = 1 nfoo = 2 lstFoo = [1] lstFoo[0] = 2 |
代码第5行中,更改list中第一个元素的值,因为list是可改变的,所以,第一个元素变更为2。其实应该说,lstFoo指向一个
包含一个对象的数组。赋值所发生的事情,是有一个新int对象被指定给lstFoo所指向的数组对象的第一个元素,但是对于lstFoo本身来说,所指向的数组对象并没有变化,只是数组对象的内容发生变化了。这个看似void*的变量所指向的对象仍旧是刚刚的那个有一个int对象的list。
Python的函数参数传递:传值?引用?
对于变量(与对象相对的概念),其实,python函数参数传递可以理解为就是变量传值操作,用C++的方式理解,就是对void*赋值。如果这个变量的值不变,我们看似就是引用,如果这个变量的值改变,我们看着像是在赋值。有点晕是吧,我们仍旧据个例子。不可变对象参数调用
1 2 3 4 5 | def ChangeInt( a ): a = 10 nfoo = 2 ChangeInt(nfoo) print nfoo #结果是2 |
可变对象参数调用
1 2 3 4 5 | def ChangeList( a ): a[0] = 10 lstFoo = [2] ChangeList(lstFoo ) print nfoo #结果是[10] |
可变对象参数调用
1 2 3 4 5 | def ChangeList( a ): a[0] = 10 lstFoo = [2] ChangeList(lstFoo ) print nfoo #结果是[10] |
恩,现在是不是对python中的变量和对象的概念有了更深入的理解了呢?
通过我上面的解释,我想大家也可以自己搞定其他类型对象的传递问题了吧。
----------------------
Python的文档对参数传递的是值还是引用没有明确说明,下面的代码没有改变值
Original
class PassByReference: def __init__(self): self.variable = 'Original' self.Change(self.variable) print self.variable def Change(self, var): var = 'Changed'
有什么方法能让通过引用来改变变量吗?
参数是通过assignment来传递的.原因是双重的:
传递的参数实际上是一个对象的引用(但是这个引用是通过值传递的)
一些数据类型是可变的,但有一些就不是.
所以:
如果传递一个可变对象到一个方法,方法就会获得那个对象的引用,而你也可以随心所欲的改变它了.但是你在方法里重新绑定了这个引用,外部是无法得知的,而当函数完成后,外界的引用依然指向原来的对象.
如果你传递一个不可变的对象到一个方法,你仍然不能在外边重新绑定引用,你连改变对象都不可以.
为了弄懂,来几个例子.
列表-可变类型
让我们试着修改当做参数传递给方法的列表: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的指向.
希望你清楚以上那些.
修改:到现在位置还没有回答"有什么方法同过引用传递变量?",让我们往下看.
我们该怎么办?
你可以返回一个新值.这不会改变传过来的值,但是能得到你想要的结果.def return_a_whole_new_string(the_string): new_string = something_to_do_with_the_old_string(the_string) return new_string # 你可以像这样调用 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 # 你可以像这样调用 wrapper = [my_string] use_a_wrapper_to_simulate_pass_by_reference(wrapper) do_something_with(wrapper[0])
虽然看起来有一点笨重,但还是达到你的效果了.
相关文章推荐
- Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- [zt]Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- Python函数参数传递:传值还是传引用
- Python的函数参数传递:传值?引用?
- python函数参数是值传递还是引用传递:取决于对象内容可变不可变
- 深入理解python中函数传递参数是值传递还是引用传递
- Python引用、浅拷贝、深拷贝 和 Numpy中的视图、副本 和 Python函数参数传递
- Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- python - python函数的参数传递是传值还是传引用?
- Python的函数参数传递:传值?引用?
- Python函数参数传递:传值还是传引用
- Python里的函数参数传递方式(表现为非值传递也非引用传递)
- python函数参数引用传递
- python函数的参数传递问题---传值还是传引用?
- Python的函数参数传递:传值?引用?