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

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

关于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中参数传递都是按“引用”传递,而非按“值”传递。

 

 


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

 

 



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

 

目录
相关文章
|
6月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
397 1
|
6月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
982 1
|
6月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
332 0
|
7月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
346 101
|
7月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
282 99
|
7月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
393 98
|
7月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
8月前
|
Python
Python 函数定义
Python 函数定义
747 155
|
9月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
706 0
|
7月前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
1129 0

推荐镜像

更多