Reference or Value?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Feb 23 18:47:46 EST 2009


En Mon, 23 Feb 2009 17:17:04 -0200, Brian Blais <bblais at bryant.edu>  
escribió:
> 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.
>>>
>>> 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.

Ok, in any case, making such claims without explanation or restricting the  
sense, only confuses the matter (which is innecesarily confusing  
already...)

-- 
Gabriel Genellina




More information about the Python-list mailing list