On Fri, 22 Sep 2017 10:27 pm, Marko Rauhamaa wrote:

> r...@zedat.fu-berlin.de (Stefan Ram):
> 
>> Marko Rauhamaa <ma...@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:

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.

The whole point of call by reference semantics is that the *compiler*, not the
programmer, tracks the variables and their scopes. The programmer just
says "swap x and y", and the compiler works out how to do it.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to