passing by refference

Tim Peters tim.one at comcast.net
Tue May 13 22:26:40 EDT 2003


[Jeremy Hylton]
>> If we care to debate terminology, then neither call-by-value nor
>> call-by-reference apply.  Python has call-by-object semantics.  As far
>> as I know, the first use of call-by-object was in a description of CLU.

[Doug Quale]
> Python is call-by-value.  The values are objects, but the argument
> passing mechanism doesn't say anything about what values are in the
> language.

Sorry, that didn't make sense.  It's true that, in CPython, pointers to
objects are passed under the covers, in C's call-by-value fashion, but
that's an implementation detail, and has no visible effect on the Python
language's semantics.  Call-by-object really is the best description of
Python's semantics.  If you do a literature search, you'll also see that
described as "call by object reference" and "call by sharing".
Call-by-value it ain't -- unless you view the implementation reference as
"the value", but that simply isn't helpful to anyone except Python's
implementors (of which Jeremy and I are two, BTW).

>>> def f(x):
...     x[:] = [-1] * 3
>>> y = [1, 2, 3]
>>> f(y)
>>> y
[-1, -1, -1]
>>>

There's no rational sense in which that can be called call-by-value.  A
similar program using C structs shows what call-by-value does mean:

#include <stdio.h>

struct s {
    int x, y, z;
};

void display(const char* tag, struct s s) {
    printf("%s %d %d %d\n", tag, s.x, s.y, s.z);
}

void f(struct s s) {
    display("inside before", s);
    s.x = s.y = s.z = -1;
    display("inside after", s);
}

void main() {
    struct s t = {1, 2, 3};
    display("before", t);
    f(t);
    display("after", t);
}

Of course that displays:

before 1 2 3
inside before 1 2 3
inside after -1 -1 -1
after 1 2 3






More information about the Python-list mailing list