passing by refference

Doug Quale quale1 at charter.net
Thu May 15 22:54:55 EDT 2003


"Fredrik Lundh" <fredrik at pythonware.com> writes:

> Doug Quale wrote:
> 
> > According to Joshua, me and R5RS.  R5RS says
> >
> > "When the procedure is later called with some actual
> > arguments, the environment in which the lambda expression was evaluated will
> > be extended by binding the variables in the formal argument list to
> > fresh locations, the corresponding actual argument values will be stored
> > in those locations, and the expressions in the body of the lambda expression
> > will be evaluated sequentially in the extended environment."
> >
> > That's call-by-value by definition.
> 
> but that's not how Python works.
> 
> in Python, the variables in the formal argument list are bound to the
> actual argument objects.  the objects are _shared_ between caller
> and callee; there are no "fresh locations" or extra "stores" involved.

No, not really.  The actual argument objects you refer to are the
argument values -- the arguments are evaluated before they are passed.
Storing the argument values in those new locations is simply creating
a new binding.  In Python,

>>> def fact(n):
>>>     if n == 1:
>>>         return n
>>>     else:
>>>         return n * fact(n - 1)
>>>
>>> fact(5)
120

Each call to fact(n) binds a new name (fresh location).  If this
weren't the case, recursive calls wouldn't work in Python.  Function
calls push stack precisely because fresh locations are used.  If no
fresh locations were needed Python wouldn't have to have a recursion
limit.

The stack was invented to make just this thing work in Algol 60 (43
years ago!).  (Algol was call-by-name, but that was decided before
they realized that c-b-n is a mistake.)

> (which, of course, is why the CLU folks called this mechanism "call-
> by-sharing".  and they were real computer scientists, working for a
> real computer science laboratory ;-)

I think call-by-sharing is unneeded terminology, but that's just my
opinion.  I've seen it applied also to Smalltalk, but it doesn't seem
to have gained much traction in the programming language theory
community.  I suspect that's because it doesn't mean anything
different than call-by-value for the languages to which it's been
applied.

> and btw, Python functions doesn't run in an extended environment,
> either.  function bodies have very limited access to the surrounding
> environment.  but that's another story.

True, but that's only because Python closures are lame. ;-) Pascal and
C are even more restrictive than Python, but they are all c-b-v.




More information about the Python-list mailing list