Question about Pass-by-object-reference?

fl rxjwg98 at gmail.com
Tue Jul 22 21:34:51 CEST 2014


On Tuesday, July 22, 2014 3:04:09 PM UTC-4, fl wrote:
Hi, 
Excuse me. I find that the OP misses some info. I rewrite it again:

I learn Python function call on tutorial. There is a link on this subject. 
http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/ 

Although it explains clearly, the figure makes me puzzled. 

""Python is different. As we know, in Python, "Object references are passed by 
value". 

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. Let's try appending again:"" 

On the figure, I understand the figure about append function. Here is the lines:

When I enter the command lines on my computer: 
>>> def append(list):
... 	list.append(1)
... 	
>>> list=[0]
>>> append(list)
>>> print(list)
[0, 1]



But I don't understand the reassign function result:

>>> def reassign(list):
... 	list=[0,1]
... 
>>> list=[0]
>>> reassign(list)
>>> print list
[0]

Questions:
1. From the tutorial explanation, both function append and reassign use 
pass-by-object-reference. Is it right?

2. The tutorial says: "The caller doesn't care if you reassign the function's 
box. Different boxes, same content." about the reassign function. I don't
understand "Different boxes, same content". It uses different boxes (I agree).
But the results are different for the caller ([0]) and the function reassign 
([0, 1]).


What is wrong with my understanding? 



Thanks a lot, 



More information about the Python-list mailing list