Finding the instance reference of an object

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 8 02:08:54 EST 2008


On Sat, 08 Nov 2008 18:31:47 +1300, greg wrote:

> Marc 'BlackJack' Rintsch wrote:
> 
>> You have said the value that is copied is a pointer to the object.
> 
> This assumes that "call by value" means "call by copying the value".
> 
> That assumption is WRONG.

Not according to my Comp Sci lecturers back in the day, and not according 
to my Pascal books.

E.g. "Programming In Pascal", 2nd Edition (1985), by Peter Grogono of 
Concordia University refers to "value" parameters and "variable" 
parameters to refer to the standard Pascal call-by-value convention and 
the call-by-reference convention you get when you declare a parameter 
with the var keyword. He writes:

"When an object is passed to a procedure by value, a local copy of it is 
made. If the object is a large array, the copying operation will make the 
program slower and will increase its memory requirements."

This was written as an undergraduate textbook. Remember that in 1985, OOP 
was far more exotic than now, and object in the above means any Pascal 
type (integer, array, set etc.) and not object as we understand it today.

Grogono doesn't explicitly use the terms "call-by-value" and "call-by-
reference", but my lecture notes from the time make it clear that the 
Melbourne University Comp Sci department understood those terms to imply 
Pascal calling semantics.


> It doesn't mean that. It means "call by ASSIGNING the value."

Which, in languages like Pascal, means COPYING the value. And that leads 
to confusion when people who understand C-B-V to mean what Pascal means 
by the term hear that Python is C-B-V.


> So, you can think of the value of an expression as being an object, as
> opposed to a reference to an object, if you want. 

That's the natural interpretation based on the ordinary meaning of the 
word "value". In a language like Python that doesn't have references, 
it's the only sensible interpretation. Otherwise you're forced to argue 
that following a statement x=1, the value of x is something that has no 
existence in Python code.


> But then you need to
> consider what it means to assign that value to a name.

But you need to do that anyway.

Here's a simple C program:

struct Rec
{
  int x;
};

struct Rec a;
struct Rec b;

int main(void)
{
  a.x = 1;
  b = a;
  printf("Before: %d %d\n", a.x, b.x);
  a.x += 1;
  printf("After: %d %d\n", a.x, b.x);
  return 0;
}


It prints:

Before: 1 1
After: 2 1



Here's a simple Python equivalent:

class Rec:
    pass

a = Rec()
a.x = 1
b = a
print "Before: %d %d" % (a.x, b.x)
a.x += 1
print "After: %d %d" % (a.x, b.x)



It prints:


Before: 1 1
After: 2 2



Anyone want to argue that assignment in Python is the same as in C?



> Obviously it doesn't mean copying the value -- it must mean making the
> name refer to the value somehow.

There's no "obviously" about it. To anyone who has learned that "call-by-
value" means that a copy is made, "obviously" it does mean copying the 
value. If you have learned a different meaning, then you will believe 
differently.


> The same thing happens when the value is passed to a function.

Sure. Calling conventions in Python are the same as name/value binding, 
except that you have a slight extra complication due to having two 
namespaces instead of one.



-- 
Steven



More information about the Python-list mailing list