Easy questions from a python beginner

Dave Angel davea at ieee.org
Fri Jul 23 07:35:57 EDT 2010


Duncan Booth wrote:
> <snip>
> Consider languages where you can easily write a swap function (or any other 
> function that updates its arguments). e.g. consider C or C#.
>
> For C your function must take pointers to the variables, so when you call 
> swap you have to make this explicit by taking the address of each variable:
>
>    foo(&x, &y);
>
> For C# the function takes references to the variables. Again you have to 
> also make this explicit at the point of call by prefixing the argument with 
> 'ref':
>
>    foo(ref x, ref y);
>
> Python is really no different: if you want a function to rebind its 
> arguments you have to make that explicit at the point of call. The only 
> difference is that in Python you make the rebinding explicit by assigning 
> to the names:
>
>   x, y = foo(x, y)
>
>
>   
I don't disagree with the overall point, but C has references (or at 
least C++ does, I don't think I've written any pure C code since 1992).  
If the function is declared to take references, the caller doesn't do a 
thing differently.

void foo(int &a, int &b);

is called by foo(x, y), and the function may indeed swap the arguments.

If I recall correctly, pascal is the same way.  The called function 
declares byref, and the caller doesn't do anything differently.

DaveA




More information about the Python-list mailing list