Finding the instance reference of an object

Arnaud Delobelle arnodel at googlemail.com
Sun Nov 9 06:17:28 EST 2008


greg <greg at cosc.canterbury.ac.nz> writes:

> 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.

Do you mean you can't do the following C++ snippet in Pascal or VB?  I
haven't used Pascal for more than 20 year and I have never used VB, so
this is a real question.

foo(int &x) {
    x = 7;
}

struct bar {
    int i;
    float j;
};

int main() {
    int a[10];
    bar b;
    // What is passed to foo below is obviously not a 'variable
    // reference' as the argument is not a variable.
    foo(a[3]); // Now a[3] == 7
    foo(b.i);  // Now b.i == 7
}

[...]

> 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.

I'm not sure that your definition of 'call by value' is widely
accepted.  If it was, then this thread wouldn't exist.

I've tried to google for an authoritative definition but it's harder
than I thought.  This definition comes up everywhere though.

    call-by-value 

    (CBV) An evaluation strategy where arguments are evaluated before
    the function or procedure is entered. Only the values of the
    arguments are passed and changes to the arguments within the called
    procedure have no effect on the actual arguments as seen by the
    caller.

[This from http://dictionary.reference.com/browse/call-by-value]

It seems to me that it more or less is the definition that most people
on this thread has been implicitly using.

-- 
Arnaud



More information about the Python-list mailing list