passing by refference

Tim Peters tim_one at email.msn.com
Wed May 14 00:54:40 EDT 2003


[Joshua Marshall]
> 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;

In Python we say y is bound to the same object, and do not say it holds the
same value.  You can say whatever you like when discussing some other
language, but it doesn't help to insist on using a different language's
terminology quirks here.

> only the list to which y holds a reference has been altered.

That's a pretty big "only" <wink>.

> Scheme is also a call-by-value language,

According to who?  You're not going to find that phrase in the Scheme
standard, although I realize that the functional programming community often
uses "call by value", and "call by name" or "call by need", to distinguish
between "eager" and "lazy" argument evaluation, respectively.  I prefer to
use the plain "eager" and "lazy" for that purpose (although in some contexts
"applicative order" and "normal order" are more natural).

>   => (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)

Obviously I don't call that call-by-value.  In a Scheme newsgroup I'd let it
pass without comment because of the above, but it's not helpful terminology
here (nor do I think it would be fruitful to try to convince Schemers that
"call by object" describes what Scheme does in a more useful way <wink>).

One of Python's predecessors was the ABC language, and that *did* have
call-by-value function semantics in the sense used here.  That kind of
distinction is important for discussing Python in its own historical context
(which includes Algol, ABC, and Pascal, all of which use call-by-value in
this sense (although Algol's call-by-name gimmick/bug is related to lazy
evaluation), but doesn't include Scheme).

when-in-denmark-speak-english-ly y'rs  - tim






More information about the Python-list mailing list