Python 关于Python函数参数传递方式的一点探索

时间:2023-03-08 17:54:29
Python 关于Python函数参数传递方式的一点探索

关于Python函数参数传递方式的一点探索

by:授客 QQ1033553122

实践代码

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ = 'laifuyu'

 

 

def function1(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)))

def function2(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)))

输出结果

Python 关于Python函数参数传递方式的一点探索

说明:比较main中,function1中接收的对象参数的值,id,不难发现:python中参数传递都是按“引用”传递,而非按“值”传递。

Python 关于Python函数参数传递方式的一点探索



说明:对比,main,function1中参数对象被修改前,修改后的值,id,不难发现:当执行
var_name = value
语句时,实际把一个新的对象,赋值给左侧的
var_name变量,,赋值后
id(var_name) = id(value),,更加有力的说明python中一切皆对象。

Python 关于Python函数参数传递方式的一点探索

说明:对比main,function2中参数对象被修改前,修改后的值,不难发现:当仅修改可变对象的组成项的值时,而不是直接对整个对象赋值的情况下,修改的是本对象自身。