别的语言中关于函数有传值和传引用的区分。
关于此,流传很广的一个说法是
他们在现象的区别之一就是值传递后的变化,受到影响的就是引用,未受到影响的就是传值。
 
在学习中,也曾碰到过这个问题,网上关于这个也是有着一些争论,各执一词。
但是官方文档中,却明确写着是call by object reference。
 
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). [1] When a function calls another function, a new local symbol table is created for that call.
 
Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list).
 
call by object reference,那么以下代码段会让人产生疑问
[root@ansible01 python]# cat func.py
def func(a):
a = 2
return a b = 1 func(b) print b
[root@ansible01 python]# python func.py
1
[root@ansible01 python]# 其中b的值并未发生变化。
要解释这个问题,可以接住python提供的一个函数id。
[root@ansible01 python]# pydoc id
Help on built-in function id in module __builtin__: id(...)
id(object) -> integer Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
将程序改写如下
[root@ansible01 python]# cat func.py
def func(a):
print "argu a id = %d , before assignment." % id(a)
a = 2
print "argu a id = %d , after assignment." % id(a)
return a b = 1
print "Variable b id = %d , before calling function func." % id(b) func(b)
print "Variable b id = %d , after calling function func." % id(b)
[root@ansible01 python]# python func.py
Variable b id = , before calling function func.
argu a id = , before assignment.
argu a id = , after assignment.
Variable b id = , after calling function func.
[root@ansible01 python]#
 
可以见到,变量b在函数调用前后的ID没有发生变化,值也未发生变化。在函数体内,在第一次赋值之前ID也未发生变化,但在赋值之后发生了变化。
正如官方文档描述的那样。
The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.
 
其实符合流传最广的那个说法是python中的复合类型,比如以下代码段
[root@ansible01 python]# cat func_1.py
a=[1] def func(b):
b[0] = 2 func(a) print a[0]
[root@ansible01 python]# python func_1.py
2
 
就如文档的脚注所言,如果传递的是可变的对象调用者可见到被调用者中间的变化。
 
所以,python中的函数传递就如官方文档所言,可叫做call by object reference,为何有不合流传较广的说法的现象出现,概由函数体内本地符号表的缘故,是一个新的引用。
 
而为啥会有区别,则要考虑python中有

可更改(mutable)与不可更改(immutable)对象

Bullet:Python的函数中参数是引用吗?的更多相关文章

  1. 多测师讲解python _函数中参数__高级讲师肖sir

    函数中讲解参数: 形参和实参的认识 函数无参数的调用 函数单个参数的调用 函数多个参数的调用 # #调试函数给默认参数传新值,则函数使用新值 # 注意:当多种参数同时出现在函数中,默认参数要放在最后的 ...

  2. python类(class)中参数self的解释说明

    python类(class)中参数self的简单解释 1.self只有在类的方法中才会有,其他函数或方法是不必带self的. 2.在调用时不必传入相应的参数.3.在类的方法中(如__init__),第 ...

  3. Python进阶-函数默认参数

    Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...

  4. python基础——函数的参数

    python基础——函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复 ...

  5. python函数中参数是如何传递的?

    python中一切皆对象,函数中参数传递的是对象的引用. 1在函数中改变变量指向的对象,即指向不同对象. 当在函数中修改传递进来的变量指向另一个对象时,实参的对象不会改变. >>> ...

  6. python函数中参数的传递

    Python唯一支持的参数传递方式是『共享传参』(call by sharing)多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Java的引用类型是这样,基本类型按值传递 ...

  7. Python函数中参数类型

    在学习Python函数的时候,函数本身的定义和调用并不是很复杂,但是函数的参数类型和用法的确有些复杂.在此做一个小结,加深理解. Python参数的定义 负责给函数提供一些必要的数据或信息,以保证函数 ...

  8. Python在函数中使用列表作为默认参数

    在学习中遇到的Python的一个坑,那就是使用列表作为默认参数. 我们知道,在Python中,列表(list)是可变对象,所以列表的内容可能会在函数内改变.另一个需要注意的是,使用列表作为函数的默认参 ...

  9. Python函数中参数* 和 ** 的区别

    * 函数接收参数为元组 例如 def myfun(*args): #相当于 def myfun(1,2,3)    ==> args 就相当于(1,2,3) for a in args: pri ...

随机推荐

  1. 发现所有的字都被加上了 <font> 标签,导致样式全部错乱

    经检查,发现我的浏览器默认打开了翻译软件!!!!!!!

  2. GitHub 高速上手 ---- 创建密钥,连接

    首先要在GitHub上创建一个帐号,因为本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以要在本地生成一个私钥和一个密钥 命令,后面跟的邮箱.要跟你自己的:增加输出的位置:能够不用设置 ...

  3. js实现存取Map结构的数据

    //控制关联表单元素是否显示 var relateItemMap = {}; for(var i=0; i<formAttributeItemList.length; i++){ var ite ...

  4. es bulk 批量删除

    bulk [root@hadoop2 ~]# cat bulk.del.es.json {"delete":{"_index":"direct_vot ...

  5. js的类库

    prototype.js https://github.com/sstephenson/prototype moment js https://github.com/moment/moment thr ...

  6. bzoj4403 序列统计——组合数学

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4403 一开始想了个 O(n) 的做法,不行啊... O(n)想法是这样的:先考虑递推,设 f ...

  7. 03、HelleBaiduMap

    D:\百度地图\百度地图\资料\百度地图与定位SDK\百度地图v3.5.0\BaiduMap_AndroidSDK_v3.5.0_All\BaiduMap_AndroidSDK_v3.5.0_Docs ...

  8. C语言作用于修饰符

    之前就遇到了坑,莫名其妙报错.   总结下: extern   声明在其他文件里 static     仅当前文件可见

  9. 洛谷P1613 跑路(最短路+倍增)

    P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...

  10. centos安装 memcached

    .安装 yum -y update yum install -y libevent libevent-devel yum install -y memcached 2.配置 vi /etc/sysco ...