By value or by reference?

Michael Hoffman at
Mon Oct 18 19:32:11 CEST 2004

Jonathan Ellis wrote:

>>By reference to an object....See the python tutorial.
> Wrong.  Here is the difference between pass by reference and pass by
> value to CS types:

Actually it is a reference to an object being passed.

>>>>def foo(a): a = 1
> ...
>>>>i = 10
>>>>print i
> With pass-by-reference, i would now be 1.  However, since python is
> pass-by-value, it remains 10.

 >>> def foo(a):
...     print id(a)
 >>> i = 10
 >>> id(i)
 >>> foo(i)

With pass-by-value, the memory location of a would be different than the 
memory location of i. However, since Python is pass-by-reference, it 
remains the same. <wink>

Alex is right that trying to shoehorn Python into a "pass-by-reference" 
or "pass-by-value" paradigm is misleading and probably not very helpful. 
In Python every variable assignment (even an assignment of a small 
integer) is an assignment of a reference. Every function call involves 
passing the values of those references.
Michael Hoffman

More information about the Python-list mailing list