Finding the instance reference of an object

Joe Strout joe at strout.net
Tue Oct 28 11:59:57 EDT 2008


On Oct 27, 2008, at 11:28 PM, Gabriel Genellina wrote:

> En Tue, 28 Oct 2008 00:58:10 -0200, greg  
> <greg at cosc.canterbury.ac.nz> escribió:
>
>> Let's look at the definitions of the terms:
>>
>> (1) Call by value: The actual parameter is an expression. It is
>>    evaluated and the result is assigned to the formal parameter.
>>    Subsequent assignments to the formal parameter do not affect
>>    the actual parameter.
>>
>> (2) Call by reference: The actual parameter is an lvalue. The
>>    formal parameter becomes an alias for the actual parameter,
>>    so that assigning to the formal parameter has the same
>>    effect as assigning to the actual parameter.
>>
>> Seems to me that (1) describes exactly how parameter passing
>> works in Python. So why insist that it's *not* call by value?

Greg is right on the money here.  It really is as simple as that.

> Those definitions are only applicable to unstructured, primitive  
> types, where the only relevant operations are "get value" and  
> "assign value".

Nonsense.  They apply to any type.  See here for an introduction to  
the topic in VB.NET:

   http://www.homeandlearn.co.uk/net/nets9p4.html

The example shown uses an Integer, but guess what -- you can pass ANY  
type either ByRef or ByVal, including object references (which are, of  
course, quite common in VB.NET).  The default is ByVal, which means  
that (as in Python) the actual parameter is an expression, and no  
assignments to it can affect the actual parameter.  It DOES NOT MATTER  
that both the formal parameter and the actual parameter may refer to  
the same object, and if that object is mutable, you can mutate that  
object.

But ByRef is another option, and if you pass an object reference  
ByRef, then the formal parameter is an alias for the actual parameter,  
and assignments to it change the actual parameter.  Python doesn't  
have this feature (nor much need for it, given its other features,  
like tuple packing/unpacking).  So, parameter passing in ByRef is  
clearly exactly the same as the default "ByVal" parameter passing in  
VB.NET... as well as Java, RB, C++ (when you remember that an object  
reference in modern languages is like a pointer in C++), and every  
other OOP language I've looked into.

Some of those languages, like Python, don't have different modes, and  
so the language designers had no call to give their one mode a name.   
So let's look at those that did have such a need, like VB.NET and  
REALbasic.  What's the default mode called?  Why, it's "ByVal".  Even  
when that value is an object reference.  This is short for "by value"  
-- it's not short for "by sharing" or "by object" or any other such  
silliness.  No such terms are needed.

There are only the two cases, which Greg quite succinctly and  
accurately described above.  One is by value, the other is by  
reference.  Python quite clearly uses by value.  Parameters are  
expressions that are evaluated, and the resulting value copied into  
the formal parameter, pure and simple.  The continued attempts to  
obfuscate this is pointless and wrong.

Best,
- Joe





More information about the Python-list mailing list