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

    MyEclipse 9.0 正式版公布 新闻 ============================================================================ ...

  2. ​网页图表Highcharts实践教程之标签组与加载动画

    ​网页图表Highcharts实践教程之标签组与加载动画 Highcharts标签组 在图表的大部分元素都提供了标签功能.但非常多时候,我们须要额外说明一些信息.这个时候借助原有的图表元素的标签功能就 ...

  3. MyReport报表系统v1.2公布

    经过多月奋战.MyReport报表系统最终完好,里程碑版本号V1.2隆重公布. 系统介绍 MyReport报表系统是基于MyReport报表引擎构建的报表开发工具平台产品.用户可以高速搭建报表中心,实 ...

  4. pygame save that Stream as video output.

    python - how to save pygame camera as video output - Stack Overflow  https://stackoverflow.com/quest ...

  5. 关于EditText的android:maxLength属性的注意事项

    一直以为在xml布局文件中对EditText添加 android:maxLength="30";属性是控制EditText字符数的.想当然的以为一个中文占2个字符,一个英文占1个字 ...

  6. Android分包MultiDex原理详解

    MultiDex的产生背景 当Android系统安装一个应用的时候,有一步是对Dex进行优化,这个过程有一个专门的工具来处理,叫DexOpt.DexOpt的执行过程是在第一次加载Dex文件的时候执行的 ...

  7. IDEA Spark Streaming 操作(套接字流)

    import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} obje ...

  8. 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。

    <script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...

  9. E20171016-mk

    chaos   n. 混乱,紊乱; (天地未出现的) 浑沌世界; 〈古〉无底深渊; 一团糟;

  10. [Swift通天遁地]八、媒体与动画-(6)使用开源类库快速实现滑入动画

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...