What other languages use the same data model as Python?

  • Thread starter Steven D'Aprano
  • Start date
G

Grant Edwards

In fairness, he's not the only one. M Harris has twice now linked to an
IBM site that describes pass-by-reference in C in terms of passing a
pointer to the argument you want as the argument. Admittedly, doing so
gives you almost the same behaviour, except that you have to dereference
the pointers yourself.

That's a pretty big difference though, and gets to the core of the
argument: it's a bit like arguing that manual cars are fitted with
exactly the same automatic transmission as auto cars, it's just that
you have to engage the clutch and shift gears yourself.

I like that analogy.

"My car has an automatic transmission except you have to shift
gears yourself with that lever and that it has a clutch operated
by that pedal instead of a hydrostatic torque converter."
 
G

Grant Edwards

... and actually, if you feel it just right and you have just the
right synchro-mesh setup, you can slide from 3rd into 4th without
every touching the clutch peddle...

That's possible on pretty much any modern automotive or small-truck
transmission, and it's not really that hard to do. I can go from 1st
up to 5th and back down to 1st without touching the clutch. I don't
as a habit do it, because it puts unnecessary wear on the syncros (and
I've had cars with pretty weak syncros -- Alfa-Romeo Spyders were
famous for the 2nd gear syncro wearing out).
and if that isn't automatic, I don't know what is....

No, that's _not_ automatic if you have to do it yourself. It's
automatic when it happens without user-intervention.

Now I think you're trolling...
 
G

Grant Edwards

Then, of course, there's references in C++. I think it's fair to call
the following "call by reference" in the sense we're talking about it
here.

void f(int& i) {
i = 5;
}
int i = 42;
f(i);

If after the call to f(i) the caller sees that i == 5, then that's
call by reference. But, we were talking about C.
Of course, C++ lets you go off the deep end with abominations like
references to pointers. Come to think of it, C++ let's you go off
the deep end in so many ways...

:)
 
H

harrismh777

Grant said:
That's what I was trying to say, but probably not as clearly. The "&"
operatore returnas a_value_ that the OP passes_by_value_ to a
function. That function then uses the "*" operator to use that value
to access some data.

I'm gonna try a D'Aprano-style bogus argument for a moment...

.... saying that 'C' does not support pass-by-reference because you have
to direct the compiler with the '&' and '*' characters is a little like
saying that

Python does not support decorations ! ...


.... because you have to direct the interpreter with some


@ bogus-decorator-syntax


I want Python to support decorations automatically !


;-)
 
H

harrismh777

Grant said:
No, that's_not_ automatic if you have to do it yourself. It's
automatic when it happens without user-intervention.

Now I think you're trolling...

.... no, I was only kidding... :)
 
H

harrismh777

Ethan said:
PS
My thanks to those who kept explaining in various ways about the
difference between language supported features and programmer constructs
-- I hadn't realized before this thread that call-by-reference was not a
language feature of C, but rather a programmer-implemented feature using
the C operators.

You are most welcome. :)
 
E

Ethan Furman

harrismh777 said:
ah, don't give up... here is a link that might help to clarify some of
these semantics... me thinks:

http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value

Indeed it does (emphasis added):

"In call-by-reference evaluation (also referred to as
pass-by-reference), a function receives an *implicit* reference to a
variable used as argument"

In C the the reference is explicit, which the article states in the next
paragraph:

"Even among languages that don't exactly support call-by-reference,
many, including C and ML, support explicit references (objects that
refer to other objects), such as pointers"

~Ethan~

PS
My thanks to those who kept explaining in various ways about the
difference between language supported features and programmer constructs
-- I hadn't realized before this thread that call-by-reference was not a
language feature of C, but rather a programmer-implemented feature using
the C operators.
 
N

Neil Cerutti

... saying that 'C' does not support pass-by-reference because
you have to direct the compiler with the '&' and '*' characters
is a little like saying that

Python does not support decorations ! ...


... because you have to direct the interpreter with some


@ bogus-decorator-syntax


I want Python to support decorations automatically !

You do have to declare decorators when defining them, but they
are called automatically when you call the decorated function,
with no special syntax required. C pointers don't automatically
dereference themselves.
 
T

Terry Reedy

But that's the sole purpose of the is operator. You either expose
those details, or you don't have an is operator at all.

Which is to say, the CPython testsuite has a CPython-specific
implementation test that uses 'is' to test that implementation detail.
 
I

Ian Kelly

I'm gonna try a D'Aprano-style bogus argument for a moment...

... saying that 'C' does not support pass-by-reference because you have to
direct the compiler with the '&' and '*' characters is a little like saying
that

    Python does not support decorations ! ...


...  because you have to direct the interpreter with some


@ bogus-decorator-syntax


    I want Python to support decorations automatically !

It seems to me that manually referencing and dereferencing in C is
more akin to decorating functions like this:

def foo(x):
do_stuff()

foo = decorate(foo)

On the other hand, the @ syntax is analogous to declaring reference
types in C++ (e.g. "int &" as opposed to "int *"). In both cases you
have to tell the interpreter / compiler that you want to use the
decoration / pass-by-reference feature, and the actual work is done
for you automatically.
 
H

harrismh777

Ian said:
On the other hand, the @ syntax is analogous to declaring reference
types in C++ (e.g. "int&" as opposed to "int *"). In both cases you
have to tell the interpreter / compiler that you want to use the
decoration / pass-by-reference feature, and the actual work is done
for you automatically.

[ @Ian, Neil ]

:) ... uh, it was worth a shot...

No, I see your point... with the slight small push-back that
dereferencing a pointer with '*' can't really be called 'work' can it?


( I know, you'll say yes, because the 'compiler' didn't do it ! )


:))
 
C

Chris Angelico

If True and False:
   waveFunction.collapse(cat)


That's going to be fun ;o)

If a process crashes and init isn't there to hear it, does it produce
a core dump?

Chris Angelico
 
D

Dennis Lee Bieber

That's a pretty big difference though, and gets to the core of the
argument: it's a bit like arguing that manual cars are fitted with
exactly the same automatic transmission as auto cars, it's just that you
have to engage the clutch and shift gears yourself.

Eeek!

Even before I had a driver's license I knew that common manual
transmissions used multiple parallel shafts with a number of
free-spinning gears and splined synchros to engage a pair... Whereas the
typical automatic used concentric planetary gear systems with friction
clutches to lock/unlock particular combinations on sun/ring/planets.

Now... Motorcycle and racing sequential shifters are a whole
different mesh...
 
N

Neil Cerutti

Destroying sanity, for fun and profit.

I was thinking more along the lines of stuff like combining the
envelope pattern (an interface class containing a pointer to an
implementation) with template classes to create type-safe
polymorphic types with specializable, decoupled implementations.

A Python programmer just feels depressed that anyone could have a
need for such innovations, though. ;)
 
C

Chris Torek

(Note: I have no dog in this fight, I would be happy with a changed
"is" or with the current one -- leaky abstractions are fine with
me, provided I am told *when* they may -- or sometimes may not --
leak. :) )

a = (1, 2, [3, 4, 5])
b = (1, 2, [3, 4, 5])
a == b True
a is b # Using the proposed definition
True

I believe that John Nagle's proposal would make "a is b" false,
because while a and b are both immutable, they contain *different*
refernces to *mutable* objects (thus failing the "identical
references to immutable objects" part of the claim).

On the other hand, should one do:

L = [3, 4, 5]
a = (1, 2, L)
b = (1, 2, L)

then "a is b" should (I say) be True under the proposal -- even
though they contain (identical) references to *mutable* objects.
Loosely speaking, we would define the "is" relation as:

(x is y) if and only if
(id(x) == id(y)
or
(x is immutable and y is immutable and
(for all components xi and yi of x, xi is yi)))

In this case, even if the tuples "a" and "b" have different id()s,
we would find that both have an immutable type, and both have
components -- in this case, numbered, subscriptable tuple elements,
but instances of immutable class types like decimal.Decimal would
have dictionaries instead -- and thus we would recursively apply
the modified "is" definition to each element. (For tuples, the
"all components" implies that the lengths must be equal; for class
instances, it implies that they need to have "is"-equal attributes,
etc.)

It's not entirely clear to me whether different immutable classes
(i.e., different types) but with identical everything-else should
compare equal under this modified "is". I.e., today:

$ cp /usr/lib/python2.?/decimal.py /tmp/deccopy.py
$ python
...False

and obviously "x is y" is currently False:
<class 'deccopy.Decimal'>

However, even though the types differ, both x and y are immutable
[%] and obviously (because I copied the code) they have all the
same operations. Since they were both created with the same starting
value, x and y will behave identically given identical treatment.
As such, it might be reasonable to ask that "x is y" be True
rather than False.

[% This is not at all obvious -- I have written an immutable class,
and it is pretty easy to accidentally mutate an instance inside
the class implementation. There is nothing to prevent this in
CPython, at least. If there were a minor bug in the decimal.Decimal
code such that x.invoke_bug() modified x, then x would *not* be
immutable, even though it is "intended to be". (As far as I know
there are no such bugs in decimal.Decimal, it's just that I had
them in my "Money" class.)]
 
C

Chris Torek

There may be some language somewhere that does pass-by-reference which
is not implemented under the hood as pointers, but I can't think of
any... 'cause like I've been saying, way down under the hood, we only
have direct and indirect memory addressing in today's processors. EOS.

There have been Fortran compilers that implemented modification of
variables via "value-result" rather than "by-reference".

This is perhaps best illustrated by some code fragments:

SUBROUTINE FOO(X, Y)
INTEGER X, Y
...
X = 3
Y = 4
RETURN

SUBROUTINE BAR(A)
FOO(A, 0)
RETURN

might compile to the equivalent of the following C code:

void foo(int *x0, int *y0) {
int x = *x0, y = *y0;
...
*x0 = x;
*y0 = y;
}

void bar(int *a0) {
int a = *a0;
int temp = 0;
foo(&a, &temp);
*a0 = a;
}

In order to allow both by-reference and value-result, Fortran
forbids the programmer to peek at the machinery. That is, the
following complete program is invalid:

SUBROUTINE PEEK(X)
INTEGER X, GOTCHA
COMMON /BLOCK/ GOTCHA
PRINT *, 'INITIALLY GOTCHA = ', GOTCHA
X = 4
PRINT *, 'AFTER X=4 GOTCHA = ', GOTCHA
RETURN

PROGRAM MAIN
INTEGER GOTCHA
COMMON /BLOCK/ GOTCHA
GOTCHA = 3
PEEK(GOTCHA)
PRINT *, 'FINALLY GOTCHA = ', GOTCHA
STOP
END

(It has been so long since I used Fortran that the above may not
be quite right in ways other than the one intended. Please forgive
small errors. :) )

The trick in "subroutine peek" is that it refers to both a "global
variable" (in Fortran, simulated with a common block) and a "dummy
variable" (as it is termed in Fortran) -- the parameter that aliases
the global variable -- in such a way that we can see *when* the
change happens. If "gotcha" starts out set to 3, remains 3 after
assignment to x, and changes to 4 after peek() returns, then peek()
effectively used value-result to change the parameter. If, on the
other hand, "gotcha" became 4 immediately after the assignment to
x, then peek() effectively used by-reference.

The key take-away here is not so much the trick by which we "peeked
inside" the implementation (although peeking *is* useful in solving
the "murder mystery" we have after some program aborts with a
core-dump or what-have-you), but rather the fact that the Fortran
language proper forbids us from peeking at all. By forbidding it
-- by making the program illegal -- the language provide implementors
the freedom to use *either* by-reference or value-result. All
valid Fortran programs behave identically under either kind of
implementation.

Like it or not, Python has similar "defined as undefined" grey
areas: one is not promised, for instance, whether the "is" operator
is always True for small integers that are equal (although it is
in CPython), nor when __del__ is called (if ever), and so on. As
with the Python-named-Monty, we have "rigidly defined areas of
doubt and uncertainty". These exist for good reasons: to allow
different implementations.
 
C

Chris Torek

Like it or not, Python has similar "defined as undefined" grey
areas: one is not promised, for instance, whether the "is" operator
is always True for small integers that are equal (although it is
in CPython), nor when __del__ is called (if ever), and so on. As
with the Python-named-Monty, we have "rigidly defined areas of
doubt and uncertainty". These exist for good reasons: to allow
different implementations.

Oops, attribution error: this comes from Douglas Adams rather
than Monty Python.
 
H

harrismh777

Chris said:
Oops, attribution error: this comes from Douglas Adams rather
than Monty Python.

Well, its certainly Monte-esq.... I like it, whoever said it.
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top