experts disagree on "call-by-reference"

Jeremy Hylton jeremy at beopen.com
Sun Jul 30 19:52:53 EDT 2000


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.

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

"In fact, CLU procedures do not share variables at all.  In addition to
there being no free variables, there is no call-by-reference.  Instead
arguments are passed 'by object'; the (pointer to the) object
resulting from evaluating the actual argument expresssion is assigned
to the formal.  (Thus pasing a parameter is just doing an assignment to
the formal.)  Similarly, a pointer to a result object is returned to
the caller.  We have found that ruling out shared variables seems to
make it easier to reason about programs.

A CLU procedure can have side effects only if the argument objects can
be modified (because it cannot access the caller's variables).  This
lead to the concept of 'mutable' objects.  Every CLU object has a
state.  The sate of some objects, such as integers and strings, cannot
change; these objects are 'immutable.'  Mutable objects (e.g., records
and arrays) can have a succession of states. [Discussion of whether
CLU should have opted for only immutable objects ala pure Lisp
omitted.]

CLU assignment causes sharing: after executing 'x := y,' variables x
and y both refer to the same object.  If this object is immutable,
programs cannot detect the sharing, but they can if the shared object
is mutable, because a modification made via one variable will be
visible via the other one."

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.

Jeremy

-- Jeremy Hylton <http://www.python.org/~jeremy/>



More information about the Python-list mailing list