Pass-by-reference : Could a C#-like approach work in Python?

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Thu Sep 11 07:15:09 EDT 2003


On Wed, 10 Sep 2003 18:58:47 -0700, Erik Max Francis <max at alcyone.com>
wrote:

>Stephen Horne wrote:
>
>> I don't agree with this.
>> 
>> If the caller has to specify the behaviour in the syntax they use for
>> the call AS WELL AS the definition (in both cases using the 'ref'
>> modifiers) then the call mechanism is 100% explicit and predictable.
>> If, given Pythons dynamism, you have an function object of the wrong
>> type (and therefore one which does not support the call mechanism you
>> are trying to use) then it will fail - just as attempting to do a
>> conventional function call on a non-function will fail.
>
>That isn't what I meant.  Look at the following code:
>
>	someFunction(thisArgument, thatArgument)
>
>With Python as it stands right now, I know that this call can never
>rebind the names thisArgument and thatArgument.  It doesn't matter what
>someFunction is, it doesn't matter what thisArgument or thatArgument
>are.  I know it can never happen.
>
>With your proposed change, now whenever I see a function call in code I
>don't know whether it's going to rebind names from the caller's side. 
>It might, it might not.  Furthermore, with Python's dynamicism, it might
>be hard to tell without running the code (since functions are first
>class objects, inspecting the function name may not help).

Wrong.

That statement will behave exactly as it always did, and CANNOT rebind
thisArgument or thatArgument even if my suggestion were implemented.
The CALLER has to EXPLICITLY allow the rebinding by using the ref
flags IN ADDITION TO specifying it in the function definition, as is
done in C# - that was the whole point.

With this call, the first argument may be rebound but not the second
(assuming the function was written to accept the arguments specified
that way).

	someFunction(ref thisArgument, thatArgument)
                      ^
                      |
              Note this explicit
             modifier in the call

However, I am still retracting the suggestion for reasons I have
already covered.





More information about the Python-list mailing list