Re: By value or by reference?

From: Alex Martelli (aleaxit_at_yahoo.com)
Date: 10/18/04


Date: Mon, 18 Oct 2004 18:04:58 +0200

Jonathan Ellis <jbellis@gmail.com> wrote:
   ...
> > By reference to an object....See the python tutorial.
>
> Wrong. Here is the difference between pass by reference and pass by
> value to CS types:
>
> >>> def foo(a): a = 1
> ...
> >>> i = 10
> >>> foo(i)
> >>> print i
>
> With pass-by-reference, i would now be 1. However, since python is
> pass-by-value, it remains 10.

...so you tell "CS types" it's pass-by-value, and they come right back
with

def bar(b): b.append(2)

z = []
bar(z)
print z

With pass-by-value, they'll say, z would still be []. However, since
what is passed is not just (a copy of) the value, but (a reference to)
the object itself, z is [2] instead.

The terminology problem may be due to the fact that, in python, the
value of a name is a reference to an object. So, you always pass the
value (no implicity copying), and that value is always a reference.

I find it simpler to explain as: the semantics of argument passing are
_exactly_ identical to that of assignment (binding) to a barename; you
can fruitfully see argument passing as local (bare) names of the called
function being assigned initial values by the caller (that's exactly
what happens, in practice). Now if you want to coin a name for that,
such as "by object reference", "by uncopied value", or whatever, be my
guest. Trying to reuse terminology that is more generally applied to
languages where "variables are boxes" to a language where "variables are
post-it tags" is, IMHO, more likely to confuse than to help.

Alex



Relevant Pages

  • Re: Whats the difference of those two ways of passing parameters
    ... Parameter passing in C is beautifully simple: ... reference", you just pass a pointer and modify the object through that. ... In languages like Java, ... int foo = 42; ...
    (comp.lang.c)
  • Re: Basic inheritance question
    ... Where is the "explicit calling of class name" exactly? ... which in python is generally called "self". ... reference to the current instance is to pass it as an argument to the ... BadThingwrt/ readbility, specially with multiparadigm languages ...
    (comp.lang.python)
  • Re: A taxonomy of types
    ... Before a representation is practical I think we need a taxonomy (so we ... dynamic typing or type inference and most languages have rules ... Target domain of reference ... Sequence of component types and names ...
    (comp.lang.misc)
  • Re: Basic inheritance question
    ... Old Java habits die slowly. ... No, seriously it isn't Java habits only, most other languages wouldn't ... That's not very far from what a Python method object does - ... reference to the current instance is to pass it as an argument to the ...
    (comp.lang.python)
  • Re: Newlisp?
    ... > Those notions do not describe if first class objects of a language are ... > passed by value or by reference. ... In Perl a variable contains a scalar (string or pointer), ... In almost all other languages a variable always points to an object, ...
    (comp.lang.lisp)

Loading