关于Python函数参数传递方式的一点探索
实践代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ ='laifuyu'
deffunction1(int_obj, float_obj, str_obj, boolean_obj, list_obj, tuple_obj, set_obj, dict_obj):
print('\nvalue of args of function1: ')
print('int_obj: %s id:%s'% (int_obj,id(int_obj)))
print('float_obj: %s id:%s'% (float_obj,id(float_obj)))
print('str_obj: %s id:%s'% (str_obj,id(str_obj)))
print('boolean_obj: %s id:%s'% (boolean_obj,id(boolean_obj)))
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
int_obj =8
float_obj =9.0
str_obj ='ishouke'
boolean_obj =True
list_obj = ['i','shou','ke']
tuple_obj = ('shou','ke','2014')
set_obj = {'shouke','2014'}
dict_obj = {'time':2014,'author':'shouke'}
print('\nvalue of obj after value changed in function1: ')
print('int_obj: %s id:%s'% (int_obj,id(int_obj)))
print('float_obj: %s id:%s'% (float_obj,id(float_obj)))
print('str_obj: %s id:%s'% (str_obj,id(str_obj)))
print('boolean_obj: %s id:%s'% (boolean_obj,id(boolean_obj)))
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
deffunction2(list_obj, tuple_obj, set_obj,dic_obj):
list_obj[2] ='2014'
tuple_obj[2][0] ='2016'
set_obj.add('2014')
dict_obj['time'] =2014
print('\nvalue of obj after value changed in function2: ')
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
if__name__ =='__main__':
int_obj =6
float_obj =7.0
str_obj ='shouke'
boolean_obj =False
list_obj = ['shou','ke','python']
tuple_obj = ('shou','ke', ['2014'])
set_obj = {'i','shouke'}
dict_obj = {'time':2016,'author':'ishouke'}
print('value of obj in main before function1 called: ')
print('int_obj: %s id:%s'% (int_obj,id(int_obj)))
print('float_obj: %s id:%s'% (float_obj,id(float_obj)))
print('str_obj: %s id:%s'% (str_obj,id(str_obj)))
print('boolean_obj: %s id:%s'% (boolean_obj,id(boolean_obj)))
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
function1(int_obj, float_obj, str_obj, boolean_obj, list_obj, tuple_obj, set_obj, dict_obj)
print('\nvalue of obj in main after function1 called: ')
print('int_obj: %s id:%s'% (int_obj,id(int_obj)))
print('float_obj: %s id:%s'% (float_obj,id(float_obj)))
print('str_obj: %s id:%s'% (str_obj,id(str_obj)))
print('boolean_obj: %s id:%s'% (boolean_obj,id(boolean_obj)))
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
function2(list_obj, tuple_obj, set_obj, dict_obj)
print('\nvalue of obj in main after function2 called: ')
print('list_obj: %s id:%s'% (list_obj,id(list_obj)))
print('tuple_obj: ',tuple_obj,' id: ',id(tuple_obj))
print('set_obj: %s id: %s'% (set_obj,id(set_obj)))
print('dict_obj: %s id: %s'% (dict_obj,id(dict_obj)))
输出结果
说明:比较main中,function1中接收的对象参数的值,id,不难发现:python中参数传递都是按“引用”传递,而非按“值”传递。
说明:对比,main,function1中参数对象被修改前,修改后的值,id,不难发现:当执行var_name = value语句时,实际把一个新的对象,赋值给左侧的var_name变量,,赋值后id(var_name) = id(value),,更加有力的说明python中一切皆对象。
说明:对比main,function2中参数对象被修改前,修改后的值,不难发现:当仅修改可变对象的组成项的值时,而不是直接对整个对象赋值的情况下,修改的是本对象自身。