Mutability of function arguments?

Mike Meyer mwm at mired.org
Thu Dec 8 09:50:10 EST 2005


Kent Johnson <kent at kentsjohnson.com> writes:
> Mike Meyer wrote:
>> "ex_ottoyuhr" <ex_ottoyuhr at hotmail.com> writes:
>>
>>>I'm trying to create a function that can take arguments, say, foo and
>>>bar, and modify the original copies of foo and bar as well as its local
>>>versions -- the equivalent of C++ funct(&foo, &bar).
>> C++'s '&' causes an argument to be passed by reference. Python does
>> that with all arguments. Any changes you make to the argument in the
>> function will be seen in the caller unless you explicitly make a copy
>> to pass.
> I would say, from the point of view of a C++ programmer, Python passes
> references by value. In other words if you think of variables as
> pointers (references) to values, and function call as passing the
> reference by value, the behaviour of Python makes sense.

While the description is right, the terminology is wrong, and places
the emphasis in the wrong place.

Your description of "passes references by value" is a description of
call by reference. C passes all arguments by value, to pass a
reference, the C programmer creates the reference to the value "by
hand", then dereferences it by hand at the other end. So C's
"call-by-reference" passes the reference by value. There's no
difference between C's call-by-reference and Python's
call-by-reference, and using different words to try and imply there is
will just cause problems further on.

The real difference is in the way names behave in the two
languages. As you put it, "variables are references to values", except
Python names don't have most of the things associated with variables
in other programming languages, so it's better to call them names. We
use "bound" to show that we're not copying a value over a fixed memory
location, hence "names are bound to values." This is the crucial
point, and the one that need to be emphasized.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list