别的语言中关于函数有传值和传引用的区分。
关于此,流传很广的一个说法是
他们在现象的区别之一就是值传递后的变化,受到影响的就是引用,未受到影响的就是传值。
 
在学习中,也曾碰到过这个问题,网上关于这个也是有着一些争论,各执一词。
但是官方文档中,却明确写着是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)对象

最新文章

  1. 如何获得APP内部资源
  2. Oracle忘记密码的处理办法
  3. Oracle基础和用户管理
  4. 安装glue,用glue批量处理图片的步骤
  5. JSP 容易弄混的几点总结
  6. php中常用的运算符
  7. JAVA的数组和输入
  8. apache访问控制设置
  9. Ohloh研究报告
  10. centos mysql 修改mysql用户密码
  11. SVM算法简单应用
  12. Redis常用命令与高级应用
  13. [内核驱动] DOS路径转化为NT路径
  14. 莫烦tensorflow(3)-Variable
  15. Android 网络请求超时处理方案
  16. hdu-1058(动态规划)
  17. uploadify IE11 不兼容问题(不显示图片)
  18. 深入理解Eureka - Eureka Client获取注册信息机制
  19. 113. 路径总和 II
  20. Report studio交叉表求指定维度和的问题

热门文章

  1. 自然语言处理中的Attention Model:是什么及为什么
  2. 我不常用的 javascript
  3. 某Android手游的lua源码逆向分析与还原
  4. 金典 SQL笔记 SQL语句汇总
  5. rel='canonical'
  6. GammaRay观察Qt程序的运行状况
  7. LaTeX 在线编辑器(LaTeX online editors)
  8. Robert 的军队
  9. poj2262 Goldbach's Conjecture——筛素数
  10. POJ3070Fibonacci