"always passes by reference"

Greg Weeks weeks at golden.dtc.hp.com
Sun Jul 30 18:11:50 EDT 2000


Martijn Faassen (m.faassen at vet.uu.nl) wrote:
: Perhaps there is some other semantics for 'value' and 'reference' stemming
: from another programming tradition that I'm not aware about, though.

There is, but it may wither away as time goes on.  I guess I'm trying to
keep it alive.  Consider the following snippet of Python code:

    def f(x):
	x = <something>

    a = <something_else>
    f(a)
    
At this point in the program, a is still <something_else>.

On the other hand, in the analogous Perl code, things are different.

    sub f {
	$_[0] = <something>;
    }

    $a = <something_else>;
    f($a);

At this point, $a is <something>.  The function f did not just receive the
pattern of bits that were written into $a.  f received the *address* of the
variable $a, dereferenced it, and called the result $_[0].  So assigning to
$_[0] is the same as assigning to $a.

Regards,
Greg



More information about the Python-list mailing list