passing by refference

Daniel Fackrell unlearned at DELETETHIS.learn2think.org
Thu May 15 15:55:46 EDT 2003


"Joshua Marshall" <joshway_without_spam at myway.com> wrote in message
news:ba0pv6$oqt$1 at ginger.mathworks.com...
> Daniel Fackrell <unlearned at deletethis.learn2think.org> wrote:
> ...
> >> Yes, I agree this is an accurate description of Python.  CLU function
> >> invocation seems to have call-by-value semantics as well (by my
> >> definition and, I believe, the standard definition).  And like python,
> >> values are object references.
>
>
> > At the machine level, all that can ever be passed to a
> > procedure/function/method/whatever is a value, so in a sense, all
languages
> > could be considered to be strictly call-by-value.
>
> No, this is not what I was implying.  The difference between
> call-by-value and other argument-passing schemes is entirely semantic.
> For example, the difference between call-by-value and
> call-by-reference can be seen by doing an assignment to a formal
> parameter.  In a call-by-reference world, this can rebind a variable
> in your caller's scope, in a call-by-value world, this does not.
> Call-by-reference is often implemented by passing a pointer behind the
> scenes.

In Python, however, a callable can directly change objects in the caller's
scope as a consequence of passing.  This can happen e.g. when a list l is
passed and the callable executes l.append().  In a call-by-value world, what
is passed to the callable is strictly a copy.  Nothing about it gives any
information which can be used to modify the argument that was passed.

Consider:

>>> def append(l, item):
...     l.append(item)
...
>>> l = [1, 2, 3]
>>> l
[1, 2, 3]
>>> append(l, 4)
>>> l
[1, 2, 3, 4]

How can this be explained by call-by-value?

As pointed out by (I believe) Fredrik Lundh and Tim Peters, the traditional
uses of the terms call-by-value and call-by-reference both fall short of
describing Python's passing scheme in important ways.

Luckily, as has also been said, we are not confined to these two terms.  As
CLU's passing model is (as far as I can tell from the small amount I've read
about it) identical to that used in Python, it seems to make sense to use
the terms that were invented for the purpose of describing CLU's passing
model, namely call-by-sharing and call-by-object.

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list