why cannot assign to function call
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Jan 10 18:28:09 EST 2009
On Sat, 10 Jan 2009 12:52:47 -0700, Joe Strout wrote:
>> What is the observable difference between converting an array to a
>> reference (pointer) to that array and passing the reference by value,
>> and passing the array by reference?
>
> The difference is whether an assignment to the formal parameter (within
> the function) affects the actual parameter (in the calling code). If it
> does, then that's pass by reference. If it does not, then that's pass
> by value.
Such a definition is incomplete. You are mischaracterising call-by-
reference as defined by a single binary state: assignment either affects
the caller, or it doesn't. If it does, it is p-b-r, if it doesn't, it's p-
b-v. According to this definition, there are no other argument passing
strategies possible. That's an enormously broad brush by which you sweep
away decades of comp sci terminology. Not just Barbara Liskov and pass-by-
object, but Algol's thunk-based pass-by-name and Haskell's pass-by-need.
In other words, you have created a false dichotomy between pass-by-
reference and pass-by-value.
There are actually three fundamental characteristics of pass-by-reference:
* passing a value by reference does not lead to the value being copied,
in contrast with pass-by-value where it does;
* modifications to the value passed by reference are visible to the
caller;
* assignments to the value passed by reference are visible to the caller.
Pascal VAR parameters have all three characteristics. Pascal non-VAR
parameters have none of them. Python parameters have two of the three. C
parameters (call-by-value) have none of them, except for arrays, where
they have all three, making arrays in C behave just like Pascal pass-by-
reference VAR parameters.
--
Steven
More information about the Python-list
mailing list