"always passes by reference"

Martijn Faassen m.faassen at vet.uu.nl
Sun Jul 30 16:45:22 EDT 2000


(Greg Weeks) <weeks at golden.dtc.hp.com> wrote:
> [I should have mentioned this in my previous response, but I missed it:]

> Martijn Faassen (m.faassen at vet.uu.nl) wrote:
> : This seems to be where the confusion comes in. When people claim Python
> : passes by value, because the *references* are being passed by value, 
> : that seems to be just stating that the only thing the computer can pass
> : in the end is numbers. If you say that, everything is by value

> Not really.  The question is *which* number do you pass to the function:
> the number written in the argument variable or the address of the argument
> variable (which is then implicitly dereferenced).  Perl passes the latter
> (call by reference), Python passes the former (call by value).  So, in Perl
> you can have:

>     $x = 0;
>     f($x);
>     print "$x\n";		# PRINTS 42

Are you sure it works like that in Perl? I am not that familiar with Perl,
but I thought it had an explicit way to do dereferencing. I thought Perl
by default passes things *by value*, though you *can* pass the reference
to a value if you want to (allowing this trick).

In Python you can have the same thing, in fact, as Python passes references
around. Except that in the case of integers, the integer object is immutable.
But take a list, for instance, and you can do it:

x = [0]

def f(x):
    x[0] = 42

f(x)
print x # prints [42]

That's because in Python everything's a reference (and thus references are
passed as well). It's just that some references point to mutable objects,
and some don't (and in the latter case the semantics is equivalent to
value semantics).

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