What other languages use the same data model as Python?

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

Python uses a data model of "name binding" and "call by object" (also
known as "call by sharing"). I trust I don't need to define my terms, but
just in case:

http://effbot.org/zone/call-by-object.htm
http://effbot.org/zone/python-objects.htm


Now, this is different from languages like C and Pascal, which is based
on variables, or Forth, which explicitly manipulates a stack. Quite
often, when people want to impress upon others that Python is not C, they
will say:

"Python's data model is different from other languages"

which is perfectly correct, if you think of C as "other languages". But
it's equally correct to say that Python's data model is the same as other
languages. As I understand it, Python and Ruby have the same data model.
So does Java, so long as you only consider objects and ignore unboxed
native values. I believe (but could be wrong) that another language of
about the same vintage as Python, Emerald, also uses the same model.
That's not surprising, because I believe that Emerald (just like Python)
was strongly influenced by CLU.

What other languages use the same, or mostly similar, data model as
Python?
 
C

Chris Rebert

Python uses a data model of "name binding" and "call by object" (also
known as "call by sharing").
As I understand it, Python and Ruby have the same data model.
So does Java, so long as you only consider objects and ignore unboxed
native values. I believe (but could be wrong) that another language of
about the same vintage as Python, Emerald, also uses the same model.
That's not surprising, because I believe that Emerald (just like Python)
was strongly influenced by CLU.

What other languages use the same, or mostly similar, data model as
Python?

According to http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing
, besides those you already listed:
Scheme, OCaml, AppleScript, and possibly VB, among "many other languages".

I can't personally vouch for the accuracy of this.

Cheers,
Chris
 
T

Terry Reedy

Python uses a data model of "name binding" and "call by object"
(also known as "call by sharing"). I trust I don't need to define my
terms, but just in case:

http://effbot.org/zone/call-by-object.htm
http://effbot.org/zone/python-objects.htm


Now, this is different from languages like C and Pascal, which is
based on variables,

Or Fortran or Basic (and I suspect, but do not know, Algol) and other
descendents: Ada?, PL/1?). In statistical languages, user-defined names
typically refer to typed data columns (eash a set of storage locations)
or user-defined functions.
"Python's data model is different from other languages"

which is perfectly correct, if you think of C as "other languages".
But it's equally correct to say that Python's data model is the same
as other languages.

You defined Python's 'data model' as having two aspects: 'name binding'
and 'call by object'. A language could match one but not the other. I
believe Lisps have name-binding, but I know not all only have
call-object. Macro calls (and earlier predecessors) are call-by-code-text.

As I understand it, Python and Ruby have the same
data model. So does Java, so long as you only consider objects and
ignore unboxed native values. I believe (but could be wrong) that
another language of about the same vintage as Python, Emerald, also
uses the same model. That's not surprising, because I believe that
Emerald (just like Python) was strongly influenced by CLU.

While Guido does not, that I know of, credit CLU as Python's direct
inspiration, I think it (and Barbara Liskov) as the originator of
Python's data model. I believe she thought of the call-by-object
semantics as something of an innovation.
What other languages use the same, or mostly similar, data model as
Python?

Natural languages. That is why I think it is better to think of Python
as an algorithm language or information-object manipulation language
rather than as just a linear-memory machine language. A linear memory
with bytes addressed from 0 to max-int or max-long is an explicit part
of the definition of assembly languages and C. It is no part of the
definition of Python.

Nice begin-a-thread post.
 
G

Gregory Ewing

Steven said:
Python uses a data model of "name binding" and "call by object" (also
known as "call by sharing").

It can be summed up in a less jargony way by saying that all
data is stored in heap-allocated objects, and variables refer
to objects rather than containing them directly. Everything
else follows from that.
What other languages use the same, or mostly similar, data model as
Python?

Pretty much any dynamically-typed language: Lisp, Scheme,
Smalltalk, Snobol, Icon, Postscript, Ruby, Lua, ...

Some languages have it for some data types but not others.
Java, VB, Objective-C come to mind.
 
G

Gregory Ewing

Terry said:
While Guido does not, that I know of, credit CLU as Python's direct
inspiration, I think it (and Barbara Liskov) as the originator of
Python's data model. I believe she thought of the call-by-object
semantics as something of an innovation.

I don't think she can claim credit for that, seeing as as Lisp
was built around it. She may have invented the *term* "call by
object" (unnecessarily, in my opinion) but the idea wasn't new.
 
T

Terry Reedy

It can be summed up in a less jargony way by saying that all
data is stored in heap-allocated objects,

This is incomprehensible jargon to some; is only (partly) true of
(typical) machine-implementations; and seems not to be true of all
objects. I believe that for CPython, builtin objects, including the
fixed arrray of ints from -5 to 256, are allocated in another data
segment (more CS jargon, which is irrelavant to human interpreters).

Evidence 1:505493696

This appears to be initialized data segment. (Someone else could take a
white box approach and look at the source. ;-)
11227616

This is heap.

Evidence 2:
Some error messages use 'heap type' to mean 'Python-coded class'
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
1 .__class__ = str
TypeError: __class__ assignment: only for heap types

http://bugs.python.org/issue4600
and variables refer to objects rather than containing them directly.
Everything else follows from that.

Would you say 'names refer to objects rather than containing them
directly'? Surely not. Using 'name' rather than the hugely overloaded
tern 'variable' automatically avoids certain misunderstandings.

A good summary might be "Python manipulates objects that are accessed
through literals, names, and expressions."
 
D

Dennis Lee Bieber

Or Fortran or Basic (and I suspect, but do not know, Algol) and other
descendents: Ada?, PL/1?). In statistical languages, user-defined names
typically refer to typed data columns (eash a set of storage locations)
or user-defined functions.
As I recall from my programming language design class (only and
intro, it was so small we met in a meeting room rather than classroom),
ALGOL was described as "call by name"; somehow the supplied argument was
"plugged into" the parameter usages... So if one did something like
call X(a+3)

and the routine was define as
X(m)

ever use of "m" in the routine evaluated the "a+3" /at that time/...
Unlike most other language which would evaluate "a+3" in the caller's
space and (FORTRAN: pass a reference to an anonymous memory space
holding the result; C: put the result itself on the stack; Python: bind
"m" to the object created by evaluating the expression)
You defined Python's 'data model' as having two aspects: 'name binding'
and 'call by object'. A language could match one but not the other. I

From my viewpoint -- there is no difference in Python...

The parameter name, defined in the routine declaration, is bound to
the object given as the argument in the call...
 
J

Jorgen Grahn

On 5/1/2011 4:45 AM, Steven D'Aprano wrote: ....

Natural languages. That is why I think it is better to think of Python
as an algorithm language or information-object manipulation language
rather than as just a linear-memory machine language.A linear memory
with bytes addressed from 0 to max-int or max-long is an explicit part
of the definition of assembly languages and C. It is no part of the
definition of Python.

It's not part of the definition of C either -- C supports segmented
memory (pre-386 Intel) and separate code/data address spaces. (Even if
most C users tend not to think of it that way.)

/Jorgen
 
G

Grant Edwards

It's not part of the definition of C either -- C supports segmented
memory (pre-386 Intel) and separate code/data address spaces. (Even if
most C users tend not to think of it that way.)

Indeed. All the C compilers I used for many years (on both PDP-11 and
80286 under Unix) assumed a segmented memory space with separate data
and text addresses spaces. More recently, the same can be said for
AVR and many other Harvard architecture machines.

The "linear memory with bytes addressed from 0 to max-int or max-long"
thing is merely an implicit assumption made by bad C programmers.
 
H

Hans Georg Schaathun

On 01 May 2011 08:45:51 GMT, Steven D'Aprano
: Python uses a data model of "name binding" and "call by object" (also
: known as "call by sharing"). I trust I don't need to define my terms, but
: just in case:

Without having the time to get my hand around exactly what this means:
Simula has three ways of transmitting arguments, namely transmission
by name, by value, and by reference. Is transmission by name the same
as call by object? Anyway, I have never seen anyone counting more than
three ways of doing this ...
 
H

Hrvoje Niksic

Steven D'Aprano said:
"Python's data model is different from other languages"

which is perfectly correct, if you think of C as "other languages". But
it's equally correct to say that Python's data model is the same as other
languages. As I understand it, Python and Ruby have the same data model.
So does Java, so long as you only consider objects[...]
What other languages use the same, or mostly similar, data model as
Python?

Count in Common Lisp and Scheme.

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

Grant Edwards

On 01 May 2011 08:45:51 GMT, Steven D'Aprano
: Python uses a data model of "name binding" and "call by object" (also
: known as "call by sharing"). I trust I don't need to define my terms, but
: just in case:

Without having the time to get my hand around exactly what this means:
Simula has three ways of transmitting arguments, namely transmission
by name, by value, and by reference. Is transmission by name the same
as call by object?

No. For example, assume the argument is a.

In call by object, the expression a is evaluated (i is evaluated,
and then used as an index to determine the object that is the ith
element of a). The callee's argument name is then bound to that
object.

In call by name, every time the callee references the argument name,
the expression a is evaluated anew. If the value of 'i' or the
binding of 'a' has changed since the time of the function call, then
the callee's argument now refers to a different object than it did at
the time of the the function call. It's rather like a macro language
(e.g. cpp) which merely performs a textual substitution of the
argument name (the difference between pass-by-name and macro
substitution is that the context of the argument evaluation is
different).
 
S

Steven D'Aprano

On 01 May 2011 08:45:51 GMT, Steven D'Aprano
: Python uses a data model of "name binding" and "call by object" (also
: known as "call by sharing"). I trust I don't need to define my terms,
but : just in case:

Without having the time to get my hand around exactly what this means:
Simula has three ways of transmitting arguments, namely transmission by
name, by value, and by reference. Is transmission by name the same as
call by object? Anyway, I have never seen anyone counting more than
three ways of doing this ...

You get credit for not falling into the trap of thinking there are only
two, call by reference and call by value, but there are *many* more than
just three. Wikipedia lists at least 13:

http://en.wikipedia.org/wiki/Evaluation_strategy


See also: http://effbot.org/zone/call-by-object.htm
 
M

Mel

Hans said:
On 01 May 2011 08:45:51 GMT, Steven D'Aprano
: Python uses a data model of "name binding" and "call by object" (also
: known as "call by sharing"). I trust I don't need to define my terms,
: but just in case:

Without having the time to get my hand around exactly what this means:
Simula has three ways of transmitting arguments, namely transmission
by name, by value, and by reference. Is transmission by name the same
as call by object? Anyway, I have never seen anyone counting more than
three ways of doing this ...

To illustrate the neither-fish-nor-fowl nature of Python calls:

mwilson@tecumseth:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information..... a_list[0] = "If you can see this, you don't have call-by-value"
.... a_list = ["If you can see this, you have call-by-reference"]
....
my_list = [None]
identify_call (my_list)
my_list
["If you can see this, you don't have call-by-value"]



so it's neither call-by-value nor call-by-reference as (e.g.) C or PL/I
programming would have it (don't know about Simula, so I am off topic,
actually.) It's not so wrong to think of Python's parameter handling as
ordinary assignments from outer namespaces to an inner namespace.

Mel.
 
G

Grant Edwards

To illustrate the neither-fish-nor-fowl nature of Python calls:

mwilson@tecumseth:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.... a_list[0] = "If you can see this, you don't have call-by-value"
... a_list = ["If you can see this, you have call-by-reference"]
...
my_list = [None]
identify_call (my_list)
my_list
["If you can see this, you don't have call-by-value"]

so it's neither call-by-value nor call-by-reference as (e.g.) C or PL/I
programming would have it (don't know about Simula, so I am off topic,
actually.) It's not so wrong to think of Python's parameter handling as
ordinary assignments from outer namespaces to an inner namespace.

As long as you think of "ordinary assignments" the Python way and not
the C or PL/I way. :)
 
H

Hans Georg Schaathun

mwilson@tecumseth:~$ python
: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
: [GCC 4.4.3] on linux2
: Type "help", "copyright", "credits" or "license" for more information.
: >>> def identify_call (a_list):
: ... a_list[0] = "If you can see this, you don't have call-by-value"
: ... a_list = ["If you can see this, you have call-by-reference"]
: ...
: >>> my_list = [None]
: >>> identify_call (my_list)
: >>> my_list
: ["If you can see this, you don't have call-by-value"]

This looks like plain old transmission by reference to me.
I.e. the functions get a reference to an object and make any
change to the object. Since the caller and the callee refer
to the same object, changes made by the callee are seen by
the caller. However, the reference cannot be changed.

With transmission by name, you would get what you call
call-by-reference; i.e. the variable passed as an argument is
changed to refer to a completely new object. In simula this
is used for output parameters.

And transmission by value is of course a copy of the data.

: so it's neither call-by-value nor call-by-reference as (e.g.) C or PL/I

I don't know PL/I; that's the sort of thing my mother deals with.
Simula explicitely offerts all three. In C you can get each of the
three, by using pointers explicitely in different ways.

Whether you use C or Simula, transmission by reference, that is what
python appears to be doing, seems to be the normal approach for any
composite data type. Thus python does not seem to do anything out of
the ordinary at all.
 
H

Hans Georg Schaathun

On 03 May 2011 15:20:42 GMT, Steven D'Aprano
: You get credit for not falling into the trap of thinking there are only
: two, call by reference and call by value, but there are *many* more than
: just three. Wikipedia lists at least 13:

Ah. Those 13 approaches aren't all mutually exclusive though.
 
C

Chris Angelico

This looks like plain old transmission by reference to me.
I.e. the functions get a reference to an object and make any
change to the object.

"Reference" being exactly what's passed around. There are now two
references to that object. Since names always contain references (not
objects), it's very easy to share mutable objects
(lists/dictionaries/etc). There's an easy way for a caller or callee
to guarantee that a mutable is safe - just slice it:

identify_call(my_list[:])

That gives the called function a shallow copy of the list, which it
can modify to its heart's content, but the original list isn't
changed. Callee can do the same, with an assignment command at the top
of the function (a_list=a_list[:]).

Chris Angelico
 
D

Devin Jeanpierre

To illustrate the neither-fish-nor-fowl nature of Python calls:
mwilson@tecumseth:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>>def identify_call (a_list):

...   a_list[0] = "If you can see this, you don't have call-by-value"
...   a_list = ["If you can see this, you have call-by-reference"]
...>>> my_list = [None]
["If you can see this, you don't have call-by-value"]

so it's neither call-by-value nor call-by-reference as (e.g.) C or PL/I
programming would have it (don't know about Simula, so I am off topic,
actually.)  It's not so wrong to think of Python's parameter handling as
ordinary assignments from outer namespaces to an inner namespace.

        Mel.

Eh, that example doesn't say what you think it does. It has the same
behavior in C: http://ideone.com/Fq09N . Python is pass-by-value in a
meaningful sense, it's just that by saying that we say that the values
being passed are references/pointers. This is maybe one level of
abstraction below what's ideal, but Scheme, Java, etc. share this
terminology. (Ruby calls it pass-by-reference AFAIK. Whatever, a rose
by any other name...)

Devin Jeanpierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top