Reference or Value?

Brian Blais bblais at bryant.edu
Mon Feb 23 14:17:04 EST 2009


On Feb 23, 2009, at 3:03 , Gabriel Genellina wrote:

> En Mon, 23 Feb 2009 03:54:16 -0200, Denis Kasak  
> <denis.kasak at gmail.com> escribió:
>
>> On Mon, Feb 23, 2009 at 5:09 AM, Steven D'Aprano
>> <steven at remove.this.cybersource.com.au> wrote:
>>> On Sun, 22 Feb 2009 13:37:27 -0300, andrew cooke wrote:
>>>
>>>> as far as i understand things, the best model is:
>>>>
>>>> 1 - everything is an object
>>>> 2 - everything is passed by reference
>>>
>>> Except that is wrong. If it were true, you could do this:
>>>
>>> def swap(x, y):
>>>    y, x = x, y
>>>
>>> a = 1
>>> b = 2
>>> swap(a, b)
>>> assert a == 2 and b == 1
>>>
>>>
>>> but you can't, it does not work. Ergo, parameter passing in  
>>> Python does
>>> not have the same semantics as languages that use pass-by- 
>>> reference, such
>>> as Pascal and Basic. That means that even if you can justify the  
>>> claim
>>> "Python is pass-by-reference" by some technical argument (and I  
>>> don't
>>> believe you can), it is misleading to make that claim without  
>>> further
>>> qualifications.
>>
>> You could, however, argue that the swap function doesn't work as
>> expected (e.g. from a Pascal or a C++ POV) simply because the
>> underlying objects aren't mutable.
>
> That's irrelevant - mutable and immutable objects are passed  
> exactly the same way.
>

I don't think they were saying that mutable and immutable objects  
were handled differently.  They are different in terms of what a  
function can do to them (mutate them or no). Thus, if you can't  
mutate them, then the caller can't see any changes in the objects by  
actions in the function so the swap won't work: it doesn't mutate the  
objects at all.  In the case of a list or a dict, then one can mutate  
them, and the changes are seen in the caller.  In both cases, the  
object itself is passed the same way.


			bb

-- 
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090223/3d27500b/attachment-0001.html>


More information about the Python-list mailing list