On Nov 7, 2008, at 12:35 PM, Terry Reedy wrote:

http://en.wikipedia.org/wiki/Call_by_something#Call_by_sharing

Call by sharing

Also known as "call by object" or "call by object-sharing" is an evaluation strategy first named by Barbara Liskov et al for the language CLU in 1974[1]. It is used by languages such as Python[2] and Iota and (as argued by some[3]) Java, although the term is not in common use by the Java community. Call-by-sharing implies that values in the language are based on objects rather than primitive types.

The semantics of call-by-sharing differ from call-by-reference in that assignments to function arguments within the function aren't visible to the caller (unlike by-reference sematics). However since the function has access to the same object as the caller (no copy is made), mutations to those objects within the function are visible to the caller, which differs from call-by-value semantics.

Although this term has widespread usage in the Python community, identical semantics in other languages such as Java and Visual Basic are often described as call-by-value, where the value is implied to be a reference to the object.

You know, people rip on Wikipedia, but I'm often surprised at how frequently it succinctly and accurately describes a topic, even when it is a topic of much controversy. The above summary seems spot on. My difficulty, I guess, is that I'm coming from the Java/VB/RB community, where the semantics are exactly the same as Python, and where it makes perfect sense to call it call-by-value (since it is a trivial generalization of the very same evaluation strategy used on other types). But I get here, and there is widespread use of this call-by-sharing term (though my initial impressions were much more disparate and confusing than the above concise summary makes it sound).

Thanks for pointing this out. I'd only found the entry on evaluation strategies, which makes no mention of call-by-sharing.

http://www.pmg.csail.mit.edu/papers/thetaref/node34.html

Call by Sharing
The caller and called routine communicate only through the argument and result objects; routines do not have access to any variables of the caller.

After the assignments of actual arguments to formal arguments, the caller and the called routine share objects. If the called routine modifies a shared object, the modification is visible to the caller on return. The names used to denote the shared objects are distinct in the caller and called routine; if a routine assigns an object to a formal argument variable, there is no effect on the caller. From the point of view of the invoked routine, the only difference between its formal argument variables and its other local variables is that the formals are initialized by its caller.

==========================
Python object *do* have access to surrounding scopes, but the second paragraph exact described Python, as does the corresponding Wikipedia entry.

Well yes, but it exactly describes VB.NET, RB, Java, and C++ as well (when talking about object types rather than primitive types).

==================================
myweb.lmu.edu/dondi/fall2004/cmsi585/subroutines-in-depth.pdf

• Call by sharing
– Used by languages where variables are already references to objects
– Parameters are references to objects, but assignments to those parameters don’t change them at the level of the caller — e.g. Java uses call- by-value
for primitives (int, char) and uses call-by-sharing for Objects

Well, not according to the Java specification, it doesn't. But these sources finally make it clear that "call-by-sharing" is just a special case of call-by-value when the value is an object reference. So, it's accurate to say that Java (and all the other languages under discussion) always use call-by-value, but some of those calls (depending on the parameter data type) may also be described as call- by-sharing.

I suppose it's reasonable that if you use a language where some types are references and some types are not, the simplest thing is to describe all calls as call-by-value (except for those with an optional by-reference syntax, like C++ and RB/.NET). But if you come from a language where all types are references, then it's reasonable to describe all calls as call-by-sharing (provided the language doesn't also support call-by-reference).

So.  How about this for a summary?

"Python uses call-by-sharing. That's a special case of call-by-value where the variables are references to objects; it is these references that are copied to the parameters, not the objects themselves. For users of other languages, this is the same semantics used for objects in Java, RB/VB.NET, and C++ when dealing with objects."

Best,
- Joe

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to