passing by refference
Joshua Marshall
jmarshal at mathworks.com
Tue May 13 23:18:56 EDT 2003
Tim Peters <tim.one at comcast.net> wrote:
...
> 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:
Let me add a couple lines to your program:
>>> def f(x):
... x[:] = [-1] * 3
>>> y = [1, 2, 3]
>>> id(y)
135466204
>>> f(y)
>>> y
[-1, -1, -1]
>>> id(y)
135466204
The variable y holds the same value before and after the call to f;
only the list to which y holds a reference has been altered.
Scheme is also a call-by-value language, yet:
=> (define (f x)
(set-car! x -1)
(set-cdr! x '(-1 -1)))
F
=> (define y '(1 2 3))
Y
=> (f y)
#unspecified
=> y
(-1 -1 -1)
More information about the Python-list
mailing list