Pass by reference ?

Martijn Faassen m.faassen at vet.uu.nl
Tue Apr 11 10:11:44 EDT 2000


Robert W. Cunningham <rcunning at acm.org> wrote:
[snip]
> None changed!  Therefore, representations the functions modified were 
> DIFFERENT from those passed, though it is clear that the correct VALUE 
> was passed.

Reference semantics is indistinguishable from value semantics if the
objects manipulated are all immutable. 

There was no modification in your example; there was only assignment.
Assignment does *not* modify an object, unless it is changing a reference
inside a mutable object (such as a[0] = 'foo').

> This behavior meets the classical definition of "Pass By Value".

Reference semantics is indistinguishable from value semantics if the
objects involved are all immutable. Some objects in Python such
as integers, string and tuples, are immutable, others can be mutated.

Reference semantics is indistinguishable from value semantics if you
use a mutable object in an immutable way (this takes lots of discipline,
though).

Assignment only changes what variables (or list elements, or dictionary
elements, or object attributes) refer to, but this indistinguishable from
value semantics if you use immutable objects or treat objects immutably.
Function or method arguments have the exact same semantics as Python
assignment.

I don't think there's any dictionary if you understand this. Python works with
references only. As long as you deal with immutable objects, or treat
objects as immutables, this is identical to value semantics, though.
It's very easy to think about. Easier than C's pointers for instance, in my 
experience.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list