Finding the instance reference of an object

George Sakkis george.sakkis at gmail.com
Tue Nov 4 21:00:35 EST 2008


On Nov 4, 6:02 pm, Joe Strout <j... at strout.net> wrote:

> I know you really want Python to be unique and special -- and it is,  
> in many ways, but this isn't one of them.  Python is an OOP language  
> whose variables contain references to objects, and where such  
> references are passed (by value) to methods, just likeJava, and like  
> the default parameter mode in VB.NET, REALbasic, and C++ (though those  
> languages also offer a by-reference mode that Python andJavado not).

To Steve's defense, he doesn't claim that Python is special; in one
reply he also disputed the claim that even Java is call-by-value. Of
course most Java programmers would have a hard time taking this claim
seriously, but at least you both agree that Python and Java have the
same call semantics, you just disagree on the name. Googling for 'java
"call by sharing"' brings back only a few results with almost half of
them being Python-related discussions. Steve is definitely in the
minority but he's not completely alone: from
http://www.cs.princeton.edu/courses/archive/fall98/cs441/Lectures/Lec15/Lec15.html:

"""
Like most pure object-oriented languages, Java only supports by "call-
by-sharing". At the implementation level, this is the same as call-by-
value. I.e., the actual parameter value is copied into the formal
parameter before executing the body. At the end of the execution of
the body, the formal parameter just goes away - no copy back occurs. I
prefer to describe it as call-by-sharing because, while you may not
replace the value via an assignment in the method body, you may change
the state of an object value. In particular, if a parameter evaluates
to an object, one can send messages to it which can change its state.
"""

I believe the main source of confusion is wrt C/C++. The following
definition has different semantics in C++ and Java, although they are
both considered call by value:

void foo(Person who) {
        who.zipcode = 12345;
}

In C++ a Person object itself is copied while in Java (and Python)
only the reference ("handler") is copied. By the way, passing a
(copied) reference is *not* equivalent to call by reference: in the
former case, the assignment of the reference to a different object has
only a local effect, i.e. it is not reflected to the caller's frame,
whine in call by ref it does.

George



More information about the Python-list mailing list