Python参数的pass by argument

说明:本文参考了How do I pass a variable by reference?这个回答,并在代码注释里加入了自己的理解。
Python函数传参,本质上类似于C++中对指针的拷贝,参数相当于T*,如果改变了指向的对象,那么修改就相当于值传递。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference
Arguments are passed by assignment.
1. the parameter passed in is actually a reference to an object 
    (but the reference is passed by value)
2. Some data types are mutable, but others aren't
outer_list是对一个list object(A)的引用
the_list是对一个list object(A)的引用
Scope1是对list object(A)的追加操作
Scope2是对the_list的赋值, 改为了对另外一个list object(B)对象

outer_string是对一个string(A)对象的引用
this_string是对一个string(A)对象的引用
对this_string的赋值, 改为了对另外一个string(B)的引用

before, outer_list =  ['one', 'two', 'three'] id: 0x7fc9492aea48
got ['one', 'two', 'three'] id: 0x7fc9492aea48
changed to ['one', 'two', 'three', 'four'] id: 0x7fc9492aea48
after, outer_list =  ['one', 'two', 'three', 'four'] id: 0x7fc9492aea48
---------------
before, outer_list =  ['one', 'two', 'three'] id: 0x7fc9492aea48
got ['one', 'two', 'three'] id: 0x7fc9492aea48
changed to ['one', 'two', 'three', 'four'] id: 0x7fc9492aef48
after, outer_list =  ['one', 'two', 'three'] id: 0x7fc9492aea48
---------------
before, outer_string =  It was many and many years ago id: 0x7fc9492ac080
got It was many and many years ago id: 0x7fc9492ac080
changed to In a kindom by the sea id: 0x7fc9492ad9c0
after, outer_string =  It was many and many years ago id: 0x7fc9492ac080
"""
####### List - a mutable type
# 传进去的是个对list对象的引用,然后对所引用的对象添加一个值,
# 传入前和传出后,参数所引用的对象都是同一个,因此效果是pass by reference
class Scope1:
    def try_to_change_list_contents(the_list):
        print('got', the_list, 'id:', hex(id(the_list)))
        the_list.append('four')
        print('changed to', the_list, 'id:', hex(id(the_list)))
    def test_pass_by_reference():
        outer_list = ['one', 'two', 'three']
        print('before, outer_list = ', outer_list, 'id:', hex(id(outer_list)))
        Scope1.try_to_change_list_contents(outer_list)
        print('after, outer_list = ', outer_list, 'id:', hex(id(outer_list)))
# 传进去的是个对list对象的引用,接下来的赋值操作,使得成为了一个临时list对象的引用
# 传入前和传出后,参数所引用的对象不是同一个,因此效果是pass by value
class Scope2:
    def try_to_change_list_reference(the_list):
        print('got', the_list, 'id:', hex(id(the_list)))
        the_list = ['one', 'two', 'three', 'four']
        print('changed to', the_list, 'id:', hex(id(the_list)))
    def test_pass_by_value():
        outer_list = ['one', 'two', 'three']
        print('before, outer_list = ', outer_list, 'id:', hex(id(outer_list)))
        Scope2.try_to_change_list_reference(outer_list)
        print('after, outer_list = ', outer_list, 'id:', hex(id(outer_list)))

####### String - an immutable type
class Scope3:
    def try_to_change_string_reference(the_string):
        print('got', the_string, 'id:', hex(id(the_string)))
        the_string = 'In a kindom by the sea'
        print('changed to', the_string, 'id:', hex(id(the_string)))
    def test_pass_by_value():
        outer_string = 'It was many and many years ago'
        print('before, outer_string = ', outer_string, 'id:', hex(id(outer_string)))
        Scope3.try_to_change_string_reference(outer_string)
        print('after, outer_string = ', outer_string, 'id:', hex(id(outer_string)))
if __name__ == '__main__':
    Scope1.test_pass_by_reference()
    print('---------------')
    Scope2.test_pass_by_value()
    print('---------------')
    Scope3.test_pass_by_value()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值