首页 > 编程语言 > 详细

Python pass-by-object-reference [TBC]

时间:2020-01-10 00:50:19      阅读:95      评论:0      收藏:0      [点我收藏+]

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/

Python pass-by-object-reference [TBC]

原文:https://www.cnblogs.com/noralee/p/12173462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!