[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code

Op 22-09-17 om 15:30 schreef Steve D'Aprano:
> On Fri, 22 Sep 2017 10:27 pm, Marko Rauhamaa wrote:
>> ram at zedat.fu-berlin.de (Stefan Ram):
>>> Marko Rauhamaa <marko at pacujo.net> writes:
>>>> swap(slot_ref(locals(), "x"), slot_ref(locals(), "y"))
>>>   You need to be able to write the call as
>>> swap( x, y )
>> Why?
> Because that's the whole point of the exercise.
> The exercise isn't to demonstrate how to swap two variables. In Python, that's
> easy:
> a, b = b, a
> The exercise is to demonstrate pass by reference semantics. That requires
> demonstrating the same semantics as the Pascal swap procedure:

No it doesn't. Pass by reference doesn't imply being able to
write a swap function. A swap function as possible in pascal
requires two conditions.

1) Pass by reference
2) Copy-over assignment.

Since python doesn't have the second, the fact that you can't
write a swap function doesn't imply you don't have pass by

> procedure swap(var a, var b):
>   begin
>     tmp := a;
>     a := b;
>     b := tmp;
>   end;
> (ignoring the type declarations for brevity).
> Call by reference semantics enable you to call swap(x, y) to swap x and y in the
> caller's scope. You don't call swap('x', 'y', scope_of_x, scope_of_y) or any
> other variant. That isn't call by reference semantics.

No it doesn't, not unless the assignment mutates by copying the value on the
right side over the variable on the left side. Your assertions about call
by reference only apply in a particular context, one where assignments mutate
the variable that is assigned to.

This is a point you continuously ignore.

Antoon Pardon.