Reference or Value?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Sun Feb 22 23:09:17 EST 2009
On Sun, 22 Feb 2009 13:37:27 -0300, andrew cooke wrote:
> as far as i understand things, the best model is:
>
> 1 - everything is an object
> 2 - everything is passed by reference
Except that is wrong. If it were true, you could do this:
def swap(x, y):
y, x = x, y
a = 1
b = 2
swap(a, b)
assert a == 2 and b == 1
but you can't, it does not work. Ergo, parameter passing in Python does
not have the same semantics as languages that use pass-by-reference, such
as Pascal and Basic. That means that even if you can justify the claim
"Python is pass-by-reference" by some technical argument (and I don't
believe you can), it is misleading to make that claim without further
qualifications.
> 3 - some objects are immutable
All objects are passed the same way, regardless of whether they are
mutable or immutable.
> 4 - some (immutable?) objects are cached/reused by the system
That's irrelevant. Caching affects the creation of new objects, but has
no effect on argument passing.
--
Steven
More information about the Python-list
mailing list