Python中函数参数传递问题【转】
1.
Python passes everything the same way, but calling it "by value" or "by reference" will not clear everything up, since Python's semantics are different than the languages for which those terms usually apply. If I was to describe it, I would say that all passing was by value, and that the value was an object reference.
Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?
It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.
Actually, what Python passes includes both arguments and return statements.
2.
Python中函数的参数传递问题,函数参数的传递往往是一个难以理解的概念,记得在C语言中有一个经典的例子如下所示:
int swap(int a,int b)
{
int temp;
temp = a;
a = b;
b = temp; return ;
} int a = ,b = ;
printf("Before Swap a = %d, b = %d\n",a,b);
swap(a,b);
printf("After Swap a= %d,b = %d\n",a,b);
>>> IntNum =
>>> Num1 = IntNum
>>> id(IntNum),id(Num1)
(, )
>>> Num2 =
>>> id(IntNum),id(Num1),id(Num2)
(, , )
>>> intNum =
>>> Num1 =
>>> Num2 =
>>> id(IntNum),id(Num1),id(Num2)
(, , )
#list
>>> list1 = [,,,,]
>>> list2 = [,,,,]
>>> id(list1),id(list2)
(, )
>>> list1[]=
>>> list1
[, , , , ]
>>> id(list1),id(list2)
(, ) #dict
>>> dict1 = {'a':,'b':,'c':,'d':}
>>> dict2 = {'a':,'b':,'c':,'d':}
>>> id(dict1),id(dict2)
(, )
>>> dict1['d'] =
>>> dict1
{'a': , 'c': , 'b': , 'd': }
>>> dict2
{'a': , 'c': , 'b': , 'd': }
>>> id(dict1),id(dict2)
(, )
def function(args):
function_block
>>> def modifier(number,string,list):
number =
string = 'GoodBye'
list = [,,]
print "Inside:", number,string,list >>> num =
>>> string = 'Hello'
>>> list = [,,]
>>> print 'Before:', num, string, list
Before: Hello [, , ]
>>> modifier(num,string,list)
Inside: GoodBye [, , ]
>>> print 'After:', num, string, list
After: Hello [, , ]
>>> def modifier(list,dict):
list[] =
dict['a'] =
print 'Inside list = %s, dict = %s' %(list,dict) >>> dict = {'a':,'b':,'c':}
>>> list = [,,,,]
>>> print 'Before list = %s, dict = %s' %(list,dict)
Before list = [, , , , ], dict = {'a': , 'c': , 'b': }
>>> modifier(list,dict)
Inside list = [, , , , ], dict = {'a': , 'c': , 'b': }
>>> print 'After list = %s, dict = %s' %(list,dict)
After list = [, , , , ], dict = {'a': , 'c': , 'b': }
>>> def swap(list):
temp = list[]
list[] = list[]
list[] = temp >>> list = [,]
>>> list
[, ]
>>> swap(list)
>>> list
[, ]
Python中函数参数传递问题【转】的更多相关文章
- python中函数参数传递的几种方法
转自 http://www.douban.com/note/13413855/ Python中函数参数的传递是通过“赋值”来传递的.但这条规则只回答了函数参数传递的“战略问题”,并没有回答“战术问题 ...
- python 中函数参数传递形式
python中函数参数的传递是通过赋值来传递的.函数参数的使用又有俩个方面值得注意:1.函数参数是如何定义的 2.在调用函数的过程中参数是如何被解析 先看第一个问题,在python中函数参数的定义主要 ...
- Python中函数参数传递问题
先上两个例子: http://python.jobbole.com/85231/ a = 1 def fun(a): a = 2 fun(a) print a # 结果为1 fun(a)中的a,可以看 ...
- Python中函数的参数传递与可变长参数
转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...
- python中的参数传递和返回值
python中的参数传递类似java,有着自己的内存回收机制,这和C++有着很大的差别. 1.函数的参数传递: >>> a = [, , ] >>> def fun ...
- python中函数参数的引用方式
值传递和引用传递时C++中的概念,在python中函数参数的传递是变量指向的对象的物理内存地址!!! python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方 ...
- Python中的参数传递问题
首先需要说明python中元组,列表,字典的区别. 列表: 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单. 例如:lst = ['arwen',123] 向list中添加项有两种方法:ap ...
- 【Python】解析Python中函数的基本使用
1.简介 在Python中定义函数的基本格式为: def <函数名>(参数列表): <函数语句> return <返回值> Python中的函数形式比较灵活,声明一 ...
- 深入理解python中函数传递参数是值传递还是引用传递
深入理解python中函数传递参数是值传递还是引用传递 目前网络上大部分博客的结论都是这样的: Python不允许程序员选择采用传值还是传 引用.Python参数传递采用的肯定是"传对象引用 ...
随机推荐
- 并查集【p2700】逐个击破
题目描述-->p2700 逐个击破 题意概括 花费最小的代价,使得一些有标记的节点不连通. 分析 我们需要花费最小代价使得原来连通的图中一些节点之间不相互连通. 贪心显然是可行的(一点也不显然 ...
- Windows 环境下 Redis 安装
1.redis官方下载地址:https://redis.io/download,redis 64位下载地址:https://github.com/MicrosoftArchive/redis/rele ...
- HDU 6315: Naive Operations
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- Android的日志工具Log
Android中的日志工具类是Log(android.util.Log),这个类提供了以下几个方法来供我们打印日志. ♦ Log.v():这个方法用于打印那些最为琐碎的,意义最小的日志信息.对应级别v ...
- [Luogu P4198]楼房重建(线段树)
题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个 ...
- [Atcoder SHPC2018] Tutorial
Link: SHPC2018 传送门 C: 一道看上去有些吓人的题目,不过$1e9$规模下的$n^m$代表肯定是可以约分的 可以发现能提供贡献的数对只有$2*(n-d)$种,那么总贡献为$2*(n-d ...
- 【线段树】bzoj3922 Karin的弹幕
设置一个值K. d<=K:建立多组线段树:d>K:暴力. 最优时间复杂度的伪计算: O(n*K*logn(建树)+m*logn(询问类型1)+m*n/K(询问类型2)+m*K*logn(修 ...
- [CEOI2017]One-Way Streets
题目大意: 给你一个无向图,现在告诉你一些点对(u,v), 要你在保证从u到v的所有路径都不变的情况下,尽可能把所有的边变成单向边, 问你可以唯一确定哪些边的方向,以及方向是从u到v还是从v到u. 思 ...
- 【Python笔记】Python语言基础
Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...
- weblogic下同域不同端口下的跨域问题解决
环境:同一台服务器,同一个Weblogic应用程序,分别建两个域,两个域IP一样,端口不同.一个域里放Web应用A,一个放Web应用B. 操作:用户访问A程序的时候,A程序会返回一个链接,让用户去 ...