A question on modification of a list via a function invocation

Antoon Pardon antoon.pardon at vub.be
Wed Sep 6 10:11:57 EDT 2017


Op 06-09-17 om 14:58 schreef Steve D'Aprano:
> On Wed, 6 Sep 2017 05:12 pm, Antoon Pardon wrote:
>
> [...]
>>> I'm not saying that we should never use this model. Its a good model. But we
>>> should be clear that it is a model of the implementation, and it describes
>>> entities which are not part of the Python language. We cannot do this:
>>>
>>>
>>>      +-----+
>>>      |     |
>>>      |  5  |
>>>      |     |
>>>      +-----+
>>>         ^
>>>         |
>>>        <x>
>>>         ^
>>>         |
>>>        <y>
>>>
>>>
>>> or any of the other things we can do in a language with references-as-values,
>>> like C or Pascal.
>> I don't agree that the above is an accurate picture of C or Pascal.
> I'm surprised. You can do this in Pascal:
>
> type
>   intpointer = ^integer;
> var
>   x: intpointer;
>   y: ^intpointer;
> begin
>   new(x);  {reserve memory in the heap for an integer, and point x at it}
>   x^ := 5;
>   y := @x;  {y points to the pointer x}
> end.
>
> which would translate to the diagram above. If you don't like the fact that I'm
> using the "address of" operator @ which is non-standard Pascal, I can write
> this instead:

No it would not translate to the above diagram. It would translate to my diagram.
All variables in pascal refer to some object (in memory), they don't refer to
other variables. If you have a pointer variable, you have a variable that refers
to an object that itself refers to an other object. This later object can be
one that is refered to directly by another variable, like code as above.

>
>
> type
>   intpointer = ^integer;
> var
>   y: ^intpointer;
> begin
>   new(y);  {reserve memory in the heap for an intpointer, and point y at it}
>   new(y^);  {reserve memory in the heap for an integer, and point y^ at it}
>   y^^ := 5;
> end.
>
> I would be shocked if C were less powerful in this regard than Pascal.

That would be result in the following diagram.


   +-----+     +-----+      +-----+
   |     |     |     |      |     |
   |  *--+---->|  *--+----->|  5  |
   |     |     |     |      |     |
   +-----+     +-----+      +-----+
      ^
      |
     <y>

>> And in case of a VAR parameter in Pascal like
>>
>> procedure f(VAR x);
>> f(y)
>>
>> one could use the following, which is the same
>> as after an assignment in python.
>>
>>
>>                   +-----+
>>                   |     |
>>                   |  7  |
>>             --->  |     |
>>            /      +-----+
>>           /
>>          /           ^
>>         /            |
>>       <x>           <y>
> No no no! Pascal VAR parameters are not the same as Python assignment! You
> cannot write a Pascal swap procedure in Python!

Yes it is. Pascal VAR parameters are exactly like Python a assignment. The
fact that you can't write a swap procedure in Python is because the
semantics of the Python assignment differs from the semantics of the Pascal
assignment. Not because the VAR parameter semantics in Pascal differ from
the parameter or assignment semantics in Python. 

The semantics of the VAR parameter in Pascal and the sematics of Python
assignment are both creating a new alias for the same object or however you
want to phrase it.

-- 
Antoon Pardon




More information about the Python-list mailing list