I'm wrong or Will we fix the ducks limp?

Marko Rauhamaa marko at pacujo.net
Thu Jun 9 05:19:31 EDT 2016


Antoon Pardon <antoon.pardon at rece.vub.ac.be>:

> Op 08-06-16 om 18:37 schreef Marko Rauhamaa:
>> I see Python as doing the exact same thing with variables as C.
>>
>> What is different is that in Python, every expression evaluates to a
>> pointer. Thus, you can only assign pointers to variables.
>
> Then you think wrong. Python has no pointers, that is an
> implementation detail.

Call it what you want, but the semantics of Python is only
understandable through references (pointers, handles, leashes,
addresses) to objects. Furthermore, there is no expression in Python
that would evaluate to a reference to a variable. While variables may
(or may not) be objects, they definitely are *not* first-class objects
because of this important limitation.

> In python when you have two variables, and you assign one to the other
> you then have two variables that share an object.

When you say A and B "share" an object, I say A and B contain a pointer
(= reference) to the same object. It's simply a question of terminology,
although I haven't seen "sharing" used in this context. A "reference"
would be a great word (as in "reference counting"), but you recently
have wanted to use that word for variables.

> In C when you have two variables and you assign one to the other you
> then have two variable each refering to their own object that are
> copies of each other.

Maybe you didn't quite understand what I have written above. In C,
expressions can evaluate to values of these types (simplified):

    int

    long

    double

    pointer

    struct

    void (debatable)

However, note that C does *not* have any expression that would evaluate
to an object of these types:

    char

    short

    float

    array

    function

Now, Python all expressions yield references:

    []         evaluates to a reference (= pointer) to a fresh empty array

    1 + 2      evaluates to a reference (= pointer) to an integer

    1 / 2      evaluates to a reference (= pointer) to a float

    ".".join   evaluates to a reference (= pointer) to a function

Both languages provide the syntax:

    lvalue = expression

In light of the languages' respective expression semantics, the
semantics of the assignment statements coincide. Both languages evaluate
the expression ("rvalue") and place the result in the left-hand slot
("lvalue").

> And yes that sharing of variables in python can be simulated by
> copying pointers in C.

A "pointer" is an abstract concept both in C and Python. C does have a
much richer pointer semantics than Python. For example, C provides
pointer arithmetics.

> Maybe the simularities between variables in python can be made
> isomorphic with a specific subset of pointer operations in C.

Now we are talking. "Isomorphic" is the only useful meaning of sameness.

> In a rather straight forward environment with classes/structs that
> have an x and y attribute, the following lines behave differently
> in C and Python.
>
>   A.x = 1;
>   A.y = 2;
>
>   B = A;
>
>   B.x = 3;
>   B.y = 4;
>
> In C the variable A will still be x:1, y:2.
> In Python the variable A will be x:3, y:4.
>
> So no, Python doesn't do the exact same thing with variables as C.

The difference is not in the variables but in the expressions. In
Python,

    1

evaluates to a pointer; in C, it evaluates to an int. Furthermore, in
Python,

    A

evaluates to a pointer; in C, it evaluates to a struct.


Marko



More information about the Python-list mailing list