What other languages use the same data model as Python?

sturlamolden sturla at molden.no
Wed May 4 10:28:16 EDT 2011


On May 3, 3:50 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:

> I would say that, considering currently most popular languages and
> platforms, Python's data model is in the majority.  It is only the
> people coming from a C++ background that tend to be confused by it.

In C++, one will ususally put class variables (objects) on the stack
or in STL containers, and use references instead of pointers. This way
one gets deterministic clean-up, and operator overloading will work as
expected. It is a common beginner mistake for people coming from Java
to use "new" anywhere in C++ code, instead of inside constructors
only. Used properly, C++ has a data model for class variables similar
to Fortran (pass-by-reference). This is complicated by the ability of C
++ to pass-by-value for backwards compatibility with C, inclusing the
use of raw pointers. This hybrid and convoluted data model of C++ is a
common source of confusion and programming mistakes, both for
programmers coming from C++ to Python or C# or vice versa.

Java is somewhat between C and C#, in that it has C semantics for
elementary types (e.g. int and float), but not for objects in general.
In C# and Python, elementary types are immutable objects, byut thet
have no special pass-by-value semantics.

Python has the same data model as Scheme. This includes that code is
an object in Python (in Python that is byte code not source code, thus
no Lisp macros). Variables are names that bind to an object. Objects
are passed as references, but names are not. "Dummy arguments" (to use
Fortran termininology) are bound to the same objects with which the
function was called, but this is not call-by-reference semantics in
the style of Fortran and C++:

In Python, the "=" operator will rebind in the local scope, as in C,
Java and C#. It will not affect anything the the calling scope (as in C
++ and Fortran). Nevertheless, this is not pass-by-value, as no copy
are made. A reference in C++ and Fortran is an alias for the variable
in the calling scope. In Python it is a new variable pointing to the
same value. This is a major difference, but a common source of error
for those that don't understand it.

( for those confused about the claimed behavior of "=" in C++: The
previous paragraph deals with reference variables in C++, not those
passed with C-style pass-by-value sematics. C++ does not always behave
as C, sometimes it behaves like Fortran and Pascal.)

Thus, Python does not pass-by-value like C or C++, nor does it pass-by-
reference like C++ or Fortran.

The semantics of Python, C# and Lisp might be described as "pass-by-
handle" if we need to put a name on it.





More information about the Python-list mailing list