Is 'everything' a refrence or isn't it?
Steven D'Aprano
steve at REMOVEMEcyber.com.au
Wed Jan 4 21:11:54 EST 2006
Mike Meyer wrote:
> Peter Hansen <peter at engcorp.com> writes:
>
>
>>Steven D'Aprano wrote:
>>
>>>Python does not have references or pointers, except internally where
>>>Python coders can not get to them. It has names and objects. Keep thinking
>>>about "call by reference" and you just confuse yourself and others. Think
>>>about names and objects and it is simple and straight-forward.
>>
>>I won't argue the point, but I would point out that the term "name" is
>>insufficient for whatever it is that is stored inside a list.
>
>
> Correct. What's stored in a list is a reference.
Nonsense. What is stored in the list is an object.
Python doesn't have pointers. You are confusing the
underlying C implementation, which implements lists as
an array of pointers, with the high-level Python code,
which doesn't even know what a pointer is. It only has
objects.
Since we are discussing the perspective from a Python
programmer's point of view, it is irrelevant what the C
implementation does. If lists held references, you
could do this:
x = 5
L = [1, 2, x]
# the third item points to the same memory location
# as x; changing it will also change x
L[2] = 7
assert x == 7
which is the behaviour that "call by reference"
implies. But it doesn't work that way in high-level
Python code.
The *reason* it doesn't work is that the object 5 is
immutable. If you only ever passed mutable objects
around, you would think Python was call by reference.
And if you only ever passed immutable objects around,
you would think Python was call by value. But since
Python has both sorts of objects, it displays both
sorts of behaviour.
--
Steven.
More information about the Python-list
mailing list