标签:
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). |
[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的值并未发生变化。
[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 = 14570296 , before calling function func. argu a id = 14570296 , before assignment. argu a id = 14570272 , after assignment. Variable b id = 14570296 , after calling function func. [root@ansible01 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
标签:
原文地址:http://www.cnblogs.com/syksky/p/4999491.html