Finding the instance reference of an object

greg greg at cosc.canterbury.ac.nz
Sun Nov 9 05:23:19 EST 2008


Arnaud Delobelle wrote:

> What's a variable reference?

It's a reference to a variable. It's what gets passed behind
the scenes when you use a VAR parameter in Pascal, or a
ByRef parameter in VB.

> What you're saying is that in the code below, when foo(q) is called
> then 'p' in foo is another name for q in main. Right?
> 
> struct point {
>     int x;
>     int y;
> }
> 
> int foo(point p) {
>     p.x = 42;
> }
> 
> int main() {
>     point q = {0, 0};
>     foo(q);
>     /* So now you're saying that q.x == 0 ? */
> }

No. Passing q by value means that the value of the expression 'q',
whatever that is in the language concerned, gets assigned to the
local variable 'p', whatever *that* means in the language concerned.

Because of the way C assignment works, the result is that p ends
up with a copy of the whole struct.

Because of the way Python assignment works, the result is that
p and q end up referring to the same object.

The difference is *entirely* due to the difference in the semantics
of assignment between the two languages. Once you've taken that
into account, there is no need to look for difference in the
parameter passing scheme.

-- 
Greg



More information about the Python-list mailing list