Pass by reference or by value?

Carsten Haese carsten at uniqsys.com
Fri Jul 13 16:42:47 EDT 2007


On Fri, 2007-07-13 at 19:22 +0000, Robert Dailey wrote:
> Correction:
> 
> I ran a few more tests and python actually does a pass by value,
> meaning that a "copy" is made

That is patently incorrect. A function call in Python *never* makes a
copy of any of its arguments.

>  and the external variable that was
> passed in remains unchanged.

That depends entirely on what kind of object the "variable" is and how
you're trying to change the "variable."

>  I actually want to know how to "pass by
> reference", in that any changes made to a parameter inside of a
> function also changes the variable passed in.

Python is Pass By Reference. Always. Period.

The root of your misunderstanding is that you don't understand how the
assignment statement works in Python. You come from the world of C where
a variable is a predefined memory location and "a=1;" means "Write the
value '1' into the memory location that is inhabited by 'a',
obliterating any contents that were previously in this memory location."

Python doesn't actually have variables. Python has objects and
namespaces, and assignment statements work very differently. This has
been widely discussed before. For more information, see
http://effbot.org/zone/python-objects.htm and the thread "Understanding
python functions" that started just yesterday on this very same list.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list