别的语言中关于函数有传值和传引用的区分。
关于此,流传很广的一个说法是
他们在现象的区别之一就是值传递后的变化,受到影响的就是引用,未受到影响的就是传值。
 
在学习中,也曾碰到过这个问题,网上关于这个也是有着一些争论,各执一词。
但是官方文档中,却明确写着是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,那么以下代码段会让人产生疑问
  1. [root@ansible01 python]# cat func.py
  2. def func(a):
  3. a = 2
  4. return a
  5.  
  6. b = 1
  7.  
  8. func(b)
  9.  
  10. print b
  11. [root@ansible01 python]# python func.py
  12. 1
  13. [root@ansible01 python]#
  14.  
  15. 其中b的值并未发生变化。
要解释这个问题,可以接住python提供的一个函数id。
  1. [root@ansible01 python]# pydoc id
  2. Help on built-in function id in module __builtin__:
  3.  
  4. id(...)
  5. id(object) -> integer
  6.  
  7. Return the identity of an object. This is guaranteed to be unique among
  8. simultaneously existing objects. (Hint: it's the object's memory address.)
将程序改写如下
  1. [root@ansible01 python]# cat func.py
  2. def func(a):
  3. print "argu a id = %d , before assignment." % id(a)
  4. a = 2
  5. print "argu a id = %d , after assignment." % id(a)
  6. return a
  7.  
  8. b = 1
  9. print "Variable b id = %d , before calling function func." % id(b)
  10.  
  11. func(b)
  12. print "Variable b id = %d , after calling function func." % id(b)
  13. [root@ansible01 python]# python func.py
  14. Variable b id = , before calling function func.
  15. argu a id = , before assignment.
  16. argu a id = , after assignment.
  17. Variable b id = , after calling function func.
  18. [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中的复合类型,比如以下代码段
  1. [root@ansible01 python]# cat func_1.py
  2. a=[1]
  3.  
  4. def func(b):
  5. b[0] = 2
  6.  
  7. func(a)
  8.  
  9. print a[0]
  10. [root@ansible01 python]# python func_1.py
  11. 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. NSDate 工具

    #import "NSDate+XMGExtension.h" @implementation NSDate (XMGExtension) /** * 是否为今天 */ - (BO ...

  2. LeetCode 249. Group Shifted Strings (群组移位字符串)$

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  3. DCS实践干货:使用Redis实现分布式锁

    场景介绍 很多互联网场景(如商品秒杀,论坛回帖盖楼等),需要用加锁的方式,以对某种资源进行顺序访问控制.如果应用服务集群部署,则涉及到对分布式应用加锁.当前分布式加锁主要有三种方式:(磁盘)数据库.缓 ...

  4. 20170623_oracle_SQL

    ============SQL分类 数据定义语言(DDL):CREATE ALERT DROP TRUNCATE 数据操纵语言(DML):INSERT UPDATE DELETE SELECT 事务控 ...

  5. Java 日期时间 Date类型,long类型,String类型表现形式的转换 (转)

    Java 日期时间 Date类型,long类型,String类型表现形式的转换 1.java.util.Date类型转换成long类型java.util.Date dt = new Date();Sy ...

  6. E20171006-hm

    trace  vt. 跟踪,追踪; 追溯,探索; 探索; 查找;          vi. 沿着一小径或道路前进; 可以追溯的;            n. 痕迹; 痕迹,踪迹; 微量,极少量; [植 ...

  7. shopnc学习

    ---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...

  8. input的type等于mage标签引发的页面提交!

    主要原因是使用了<input type="image",而且放在了<form中. <input type="image"标签类似<inp ...

  9. LuaBridge

    不能直接公开基类的函数,必须单独公开基类,并声明继承关系  deriveClass<Player, BaseController>("Player") 直接公开基类的函 ...

  10. 关于 node.js的request事件

    下面展示的是一个ajax一部提交的服务器端处理过程.node创建一个web服务器,并侦听8080端口.对于服务器,我们为其绑定了request事件,对于请求对象,我们为它绑定了data和end事件: ...