A function receives a reference to (and will access) the same object in memory as used by the caller.

However, it does not receive the box that the caller is storing this object in; as in pass-by-value,

the function provides its own box and creates a new variable for itself.

  Box Object
Pass-By-Reference Same box Same Object
Pass-By-Value Different box Different value
Pass-By-Object Different box Same object

 

 

 

dis.dis() prints the byte-code instruction.

list += [1,2]  : INPLACE_ADD

list = list + [1,2]   : BINARY_ADD

Python gives types a way to handle += specially, by creating an __iadd__ method as well as an __add__.

The intention is that mutable types, like list, will mutate themselves in __iadd__ (and then return self, unless you\’re doing something very tricky)

Referenced by: 

1. https://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python

2.  https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/

版权声明:本文为noralee原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/noralee/p/12173462.html