passing by refference

Tim Peters tim.one at comcast.net
Fri May 16 00:54:06 EDT 2003


[Doug Quale]
> (I had looked up some of your earlier posts on this issue after I
> entered this morass.  Google says that "call-by-object-reference" is
> most popular recently in reference to Python, often in threads that
> you have participated in in this group.  The term is not unknown in
> published computer science research, but it's rare.)

Sorry, I didn't mean a literature search on call-by-object-reference, I
meant a literature search on call-by-value.  You can't come away from such a
search still believing there's a single "true meaning" for that phrase.  Or,
if you can, there's no hope for you <wink>.

Even the FOLDOC definition people started with here:

    changes to the arguments within the called procedure have no
    effect on the actual arguments as seen by the caller

doesn't match your preferred meaning, except by virtue of extreme
logic-chopping about what "no effect on the actual arguments" means.  I'm
afraid I think it's plainly unreasonable to claim that, when I do

x = [1, 2, 3]
f(x)
print x

and see [-1, -1, -1], the f(x) call can be said to "have no effect on the
actual arguments as seen by the caller".  To get away with that trick
requires stripping words of all reasonable meaning.  BTW, the FOLDOC
definition does match the most widely accepted meaning for call-by-value
from my college days (although there was more than one meaning in common use
then too).  I believe the phrase has been "elaborated" so much in succeeding
years that a different meaning is more common now, and the phrase is
correspondingly less useful than it once was.

> I'm afraid I can't determine the meaning of technical terms like
> "call-by-value" by what any normal person thinks it means.  Like any
> specialized technical terminology it means what expert practicioners
> in the field agree it means.

And when they don't agree on a single definition?  They don't in this case,
but it's not a religious issue to them either, and neither is it a barrier
to communication when the differences are understood.

> This is the same in law, medicine, mathematics, etc.  I'm sorry that
> so many people find this troubling, but I don't tell a biologist that
> he's crazy to say that the giant panda is more closely related to the
> racoon family than the bear family just because it's my hobbyist
> opinion that it's obvious that a panda is a bear.  (I read recently
> that there is more evidence that the giant panda is more closely
> related to the bear than the racoon, but apparently the issue still
> isn't settled definitively.)

That was fun to read (thanks!  btw, it's a bear <wink>), but I don't see the
relevance.

>> Even so, in no case does it have enough explanatory power to answer
>> most practical questions about the semantics of calls in Python.

> What practical question can it not explain about calls in Python?  It
> very easily explains the example you gave before.

Actually not:  virtually all of the explanation lied in dragging in r-value
and l-value concepts.  Occam's Razor is what opposes that:  all arguments in
Python are passed by object identity, some objects are mutable, others
aren't, and an explanation built on those terms is *useful* in Python life.
You won't hear about r-values and l-values again in Python, so it's not
helpful here to drag them in.  Of course if you were trained to think of
everything in terms or r- and l-values, it would be helpful to you -- but
most people haven't been so trained.

> ...
> Argument passing in Java and C are no different from Python.

Given the meanings you insist that words have, possibly so.  It's an easily
observable fact that people coming from C have a hard time learning Python's
calling semantics, though, so that should give you pause about whether
you're constructing a useful model of reality.  Python and Java are indeed
close, and people moving from C to Java have a hard time learning Java's
calling semantics too; people moving between Python and Java don't stumble
over this.

> The same mutation that you showed in your Python example occurs there
> too.

It depends on whether a pointer or structure is passed in C.  There's no
such distinction in Python.

> Do you think that "call by object reference" explains them?

In Python, yes.  C doesn't use call-by-object, so "no" to that part.

> Java and C are both usually considered to be call-by-value, but you
> seem to have a different opinion.

You're not hearing:  I'm not arguing for any specific meaning of
call-by-value.  I gave up on that futile quest in the 1900s.  I understand
that Java is described by its creators as call-by-value, and I understand
what they mean by that.  I've never taken a compsci course that mentioned
Java (it was invented long after my college days), but 5 minutes' googling
turned up many sets of university lecture notes on Java.  Google's top hit
of that sort is here:

    www.d.umn.edu/~rmaclin/cs5641/Notes/Lecture13.pdf

It claims that "everything is call by value" in Java, then trips over itself
with the convoluted "but for objects it's really a copy of a reference"
business, and ends with the incorrect claim that "but the pointer to the
object can then also be changed".

The second hit was:

    http://www.cs.wright.edu/people/faculty/rrea/jmethod.html

This one claims:

    Java provides both a Call by Value and a Call by Reference capability.
    Call by Value means that the value is passed to the method and any
    changes to that passed value will NOT change the value in the calling
    method.  Any primative [sic] variable that is passed such an [sic] an
    integer, a float, and so on is automatically passed by value.  This
    is a safe, but limiting way to pass a parameter.

    Call by Reference means that the called method can change the value
    in the calling method. If you pass an Object, it is automatically
    passed by reference.  We will get to objects in the next chapter.

I know what he means too, and expect that his students will come out with a
clearer understanding of how Java actually works.

The third:

    http://www.sor.princeton.edu/~rvdb/201/lectures/lec22.pdf

says Java is call-by-value but then does the usual dance exempting objects
from the implications of that, and goes on to say that C/C++ structs are
"the real thing" when it comes to call-by-value.  So I suspect that
Professor Vanderbei would be happier if he didn't feel compelled to say that
everything in Java is call-by-value.

A few more down and you get to Philip Wadler's home page (which happens to
mention Java for an irrelevant reason), and you'll crawl on your belly for a
month trying to figure out what *he* means by "call-by-value" <0.9 wink>.

And on it goes, seesawing between lecturers who stick to the
everything-is-call-by-value-in-Java party line then scrambling to explain
that object arguments in Java aren't really copied, and those who ignore the
party line, sticking to the more traditional distinction that primitive
types in Java are passed by value but that class objects and arrays are
passed by reference.

I do believe they would benefit by dropping that whole mess and embracing
call-by-object terminology instead.  Short of that, I don't care what they
want to say call-by-value means.

> ...
> Henry Baker is a very bright guy, but I'm not on the bus with this one.

No kidding <wink>.

> I didn't just invent a definition for call-by-value based on what I
> thought it should mean.  I learned the definition at school from
> professor Fischer.  Professor Fischer researches computer programming
> languages, and I presume that he taught his class the definition of
> the term as it is used in the field.

I'm sure he taught the definition the subculture he belongs to uses most
often.  Fine by me.  Believing that's the only definition in wide use
remains silly, though.  You're also free to believe that only people who
embrace Prof. Fischer's definition in all respects are expert practitioners,
but the world you live in shrinks with each such demand <0.5 wink>.  I have
a bold suggestion:  ask Prof. Fischer whether *he* believes there's only one
true defintion of this term (BTW, is this Charles Fischer of UW Madison?  if
so, I'd say his specialty is more compiler design than language design; and
if you can, get him to argue about this with Baker on some Lisp list
<wink>).

> Here is what Ravi Sethi says in "Programming Languages: Concepts and
> Constructs": ...

Sorry, I'm spacing out on this, and am out of time for this anyway.  You
don't need to convince me that many people in the field embrace the
definition you like -- I knew that going into this.  It doesn't change that
it's not universally accepted, or that I strongly doubt that any of the
authorities you've been exposed to would claim that this is a matter of One
Objective Truth.  Indeed, the dueling quotes game is proof that it isn't.
Unlike biologists arguing over the giant panda, we're not studying physical
reality here -- outside the mathematical foundations of compsci, words are
chosen mostly for perceived utility, and those perceptions vary (for good
reasons) across groups.

> ...
> I have no objection to calling Python argument passing
> call-by-object-reference.  (I would be curious to know why you think
> that is different from call-by-value in a language like Python where
> r-values are object references.)

I'm not answering this one again:  it's a matter of what's *helpful*, as
I've explained several times already.  The mess I found in the Java lecture
notes is typical of what comes from *not* breaking away from these old
overloaded phrases.  At heart, saying "call by object" invites exploration,
while saying "call by value" invites confusion.  Nobody except a computer
newbie comes to Python without *some* idea of what "call by value" means,
and they really don't have the same ideas.  Tedious lectures opposing fixed
ideas are far less productive than approaching it from an angle they don't
have preconceived notions about.

> On the other hand, if someone says Python argument passing is call-by-
> value I don't recommend telling him he's wrong as Aahz did what seems
> like thousands of messages ago.

By the definition he was taught (you're not the only one here to attend a
class <wink>), I expect it is wrong.  Then each eventually figures out what
the other meant, and it ends with a group hug <wink>.

> I have to say that your posts always make me smile.  comp.lang.python
> is a remarkably friendly forum and I apologize to all for a cranky
> post or two I made earlier.

That's very gracious, and appreciated.  Time to write some Python code.






More information about the Python-list mailing list