By value or by reference?
Michael Hoffman
m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Mon Oct 18 13:32:11 EDT 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
>>>>foo(i)
>>>>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)
168377924
>>> foo(i)
168377924
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