What other languages use the same data model as Python?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 5 10:10:27 EDT 2011


On Wed, 04 May 2011 20:11:02 -0500, harrismh777 wrote:

> These definitions go all the way back before the 8080, or the 6502, 8 
> bit processors. Pass by reference has 'always' meant pass by using a 
> memory address (indirect addressing);  a reference has always been a 
> memory pointer.

That's not a definition. That's an implementation.

Some day, we'll be using quantum computers without memory addresses, or 
DNA computers, or some version of Babbage's Difference Engine (perhaps a 
trillion of them in the volume of a match-head, tiny nano computing 
devices... who knows?). Whatever it is, whether or not it has concepts of 
"memory address" or "memory pointer", it will still be possible to 
represent data indirectly via *some* mechanism.


> If I call a function in C, and pass-by-value, the data's 'value' is 
> placed on the stack in a stack-frame, as a 'value' parm... its a copy
> of the actual data in memory.

Correct.

> If I call a function in C, and pass-by-reference, the data's 'address' 

C doesn't do pass by reference. There is no way to declare a parameter to 
a function as a by-reference parameter. You can only simulate it by hand, 
by passing a pointer as data, pointing to what you *really* want as data, 
and dereferencing it yourself. But the pointer itself is passed by value: 
the address is copied onto the stack, just like any other piece of data 
would be.

(The Python equivalent is to pass a list containing the object. If you 
want call-by-reference behaviour without the convenience of language 
support for it, you can have it.)

Pascal, on the other hand, does do pass by reference. If you declare a 
"var" parameter, you then call the function with the variable you intend, 
and the compiler handles everything:

function foo(x: int, var y: int): int;
  begin
    foo := x + y;
    y := 0;
    x := 0;
  end;

a := 1;
b := 2;
c := foo(a, b);

After calling foo, the variable a remains 1, but the variable b is now 0. 
The compiler is smart enough to figure out what to do behind the scenes 
to make it all work.

We're not discussing what you, the coder, can do. Given any Turing-
complete language, you can (with sufficient cleverness and hard-work) do 
anything any other Turing-complete language can do. We're discussing what 
the compiler does, and for C, that is purely call by value.

Let me put it this way... old, unstructured BASIC has GOTOs and line 
numbers, correct? And Python doesn't, correct? But you could write a 
BASIC interpreter in Python, and call that interpreter from your Python 
code... therefore Python has line numbers and GOTOs, no?

No. Of course not. We're discussing *language features*, and GOTO is not 
a language feature of Python. Neither is call by reference a language 
feature of C, or Python either for that matter, but it is a language 
feature of VB and Pascal. 

The only difference between the two scenarios is that writing a BASIC 
interpreter is a tad harder than dereferencing a pointer, but that's just 
a matter of degree, not of kind.



-- 
Steven



More information about the Python-list mailing list