Pass by reference or by value?

James Stroud jstroud at mbi.ucla.edu
Fri Jul 13 21:34:47 CEST 2007


Robert Dailey wrote:
> 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.

Pass a mutable type.

py> class C(object):
...   def __repr__(self):
...     return str(self.value)
...   def __init__(self, v):
...     self.value = v
...
py> c = C(4)
py> c
4
py> def doit(v):
...   v.value = v.value * 2
...
py> doit(c)
py> c
8

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list