Pass by reference ?
Robert W. Cunningham
rcunning at acm.org
Wed Apr 5 11:10:13 EDT 2000
Jacek Generowicz wrote:
> Jacek Generowicz wrote:
>
> > Hi,
> >
> > I'm just trying to familiarize myself with Python.
> > In Magnus Lie Hatland's Instant Python, he
> > mentions that `all parameters in Python are passed
> > by reference'.
>
> [snip]
>
> Thank you for your answers.
>
> I would welcome a critique of the following summary:
>
> Python variables are implemented as references to
> values.
>
> Assignment (potentially) alters the reference rather
> than the value to which it refers.
>
> Objects can be classified into two sets: mutalble and
> immutable.
>
> Mutation of a mautable passed into a function will be
> reflected in the mutation of the object which was
> passed into the function; achieving similar behaviour
> for immutables is more complicated.
>
> Jacek
As a Python newbie, this sounds about right to me. In my own mind, to
escape the ambiguity of how "Pass By Reference" and "Pass By Value" are
discussed in Computer Science, and how those same terms are (mis)used
when discussing Python, I've been using the idea of "Pass By Clone".
In essence, it seems to me Python and traditional CS meanings of these
phrases are cleanly separated by one level of abstraction. And, given
that the two types of passing are themselves separated by one level of
abstraction (pass the data vs. pass a pointer to the data), confusion
seems inevitable.
Python can either 1) Pass a reference to the original data, or 2) Pass a
reference to a copy of the original data. At no time does Python "pass
the data" directly (as in the "C" sense of, say, a register variable),
since everything is treated as though it were (in some sense) an object
(even if not "really" an object of the First Water).
Which, when mapped to conventional CS terminology (as extended by Moi),
becomes "Pass By Reference" and "Pass By Clone".
Clearly, "Pass By Clone" makes the original data item completely
inaccessible within the called function (ignoring bytehacks for now),
and thus immutable. For me, this helps to clarify things, since Python
literally passes only references.
But, of course, that depends: Is this a clarification, an obfuscation,
or Just Plain Wrong?
Finished-the-Python-tutorial-yesterday-and-the-FAQ-today-ly yours,
-BobC
More information about the Python-list
mailing list