experts disagree on "call-by-reference"

Martijn Faassen m.faassen at vet.uu.nl
Mon Jul 31 06:00:50 EDT 2000


Jeremy Hylton <jeremy at beopen.com> wrote:
> Here are two other examples that may illuminate Python's parameter
> passing mechanism.

> The C++ Programming Language (Stroustrup), 3rd ed:

> The section of references (5.5, p. 97-100) has a simple example of
> parameter passing with references.

>     void increment(int& aa) { aa++; }

>     void f()
>     {
>         int x = 1;
>         increment(x); // x = 2
>     }

> There is clearly no feature in Python to accomplish the same thing.
> The call-by-reference allows the variable aa in increment to serve as
> an alternate name for x in f.  A change to aa results in a change to
> x.

I think this example is misleading, as you used an integer, which 
is immutable in Python. For mutable variables you can accomplish much
the same thing; the difference with this C++ example is that the underlying
value can be changed directly (integers are mutable in C++!).

Another difference is that in Python, *all* variables behave as if they
were C++ 'references' (or C pointers with implicit dereferencing).

In the C++ example there is a reference-taking going on as soon as the
increment() function is called; in Python no reference needs to be taken
as it's already a reference, always.

> "A History of Clu" (Liskov) in History of Programming Languages
> (Bergin & Gibson, eds.), p. 483-4:
[snip]

> Given this extend description of CLU parameter passing, I believe it
> would be fair to say that Python is also a call-by-object language.

Yes, the description seems near-identical to the way Python works. 

I think the confusion arises in the 'call/pass by' part of the expression
'call/pass by reference'. 'call by reference' at least for many seems
to imply that a reference is taken as soon as the call happens
(as in the C++ example, for instance, and I believe also in Perl from
what I've seen). In Python, this never happens; everything is a reference
(to an object) already, so the references are simply passed along.

So you could say that Python has reference semantics, and that references
are the things that always are used and passed. But 'call by reference'
definitely seems to carry unwanted connotations (the taking of a reference),
and 'pass by reference' seems to have this trouble as well (though I'd
say less so). Even so, references are the things that are passed.

If the 'call-by-object' terminology is standard, then I'd agree Python
is call by object. But that doesn't mean talking about references isn't
useful; it would be in CLU too I presume from this description.

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