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

    js的函数传参的方式是按值传递,正常情况下,改变函数参数的值,并不会对函数外部的变量造成影响.例如: 'use strict';var list = [1, 2, 3]; list.forEach(f ...

  2. 华为FusionSphere概述——计算资源、存储资源、网络资源的虚拟化,同时对这些虚拟资源进行集中调度和管理

    华为FusionSphere概述 FusionSphere是华为自主知识产权的云操作系统,集虚拟化平台和云管理特性于一身,让云计算平台建设和使用更加简捷,专门满足企业和运营商客户云计算的需求.华为云操 ...

  3. 【POJ 3974】 Palindrome

    [题目链接] http://poj.org/problem?id=3974 [算法] 解法1 : 字符串哈希 我们可以分别考虑奇回文子串和偶回文子串,从前往后扫描字符串,然后二分答案,检验可以用哈希 ...

  4. AJAX-responseXML 属性

    如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,需要使用 responseXML 属性 cd_catalog.xml: <CATALOG><CD><T ...

  5. MyEclipse个性设置

    MyEclipse个性设置 (1)Myeclipse 打开 jsp 的默认编辑器不好,会同时打开预览.所以做如下更改 Windows–>Perferences–>General–>E ...

  6. Java经典算法之选择排序(Select Sort)

    思路:就是把所有数据项扫描一遍,挑出最小的那个和最左边的交换位置,即放到0位置.现在最左边的就是有序得了,不需要在交换位置,再次扫描数据时就是从1开始,还是寻找最小的和1交换位置,直到所有数据都是有序 ...

  7. 灾备还原之gitlab

    灾备还原之gitlab 备份情景:服务器A架设了gitlab,定期通过duplicity发送加密备份给B服务器,现在由于某种情况生产机器A完全无法访问(主机商跑路?硬盘冒烟?服务器BOOM了?),本地 ...

  8. web api初学

    据说web api的作用和wcf的一样,只是比wcf更简单而已,具体如何我也不清楚,毕竟不是做学术研究的,我只是通过简单的例子来学习web api.能做的只需要知其然,不必管其所以然.当然有兴趣的可以 ...

  9. firefox 附加组件栏安装

    firefox 在升级到 30的版本后,发现附加组件栏不兼容了. 搜索组件,add-on bar 会得到一个 new add-on bar的组件,安装完后发现上面不显示ip, 后来才发现,应该安装Th ...

  10. hdu2639,第K优决策

    在dp问题中如果遇到问题,没有什么是加一维度不能解决的,如果不能,再加一维度. #include<iostream> #include<cstring> #include< ...