What other languages use the same data model as Python?

sturlamolden sturla at molden.no
Wed May 4 12:40:12 EDT 2011


On May 4, 5:40 pm, Michael Torrie <torr... at gmail.com> wrote:

> Which is exactly what the code showed.  The first one isn't a mistake.
> You just read it wrong.

No, I read "call-by-value" but it does not make a copy. Call-by-value
dictates a deep copy or copy-on-write. Python does neither. Python
pass a handle to the object, not a handle to a copy of the object. If
you want to see call-by-value in practice, take a look at MATLAB,
SciLab or Octave; or consider what C++ copy constructors do in
function calls with value types.

The first one is indeed a mistake. An object has a value. A name binds
to an object, not to a value. If Python did pass-by-value, the string
would be inserted in an object (here: a list) with the same value
(e.g. empty list), it would not modify the same object by which you
called the function.

I think you understand what Python does, but not what call-by-value
would do.


C++ tells you the difference:


// copy constructor is invoked
// x is a copy of the argument's value
// this is call-by-value

void foobar1(Object x);


// no copy is taken
// x is a logical alias of the argument
// this is call-by-reference

void foobar2(Object &x);


// x is a pointer, not an object
// x is a copy of another pointer
// this is similar to Python sematics
// the pointer is passed by value, not the pointee

// in C, this is sometimes called call-by-reference
// as there are no reference types, but it's not

void foobar3(Object *x);



Sturla







More information about the Python-list mailing list