Re: Terminology: “reference” versus “pointer”

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Sep 12 02:05:06 EDT 2015


On 12/09/2015 06:42, Random832 wrote:
> Ben Finney <ben+python at benfinney.id.au> writes:
>> The reference value is inaccessible to the program, it can only be used
>> to get at the referenced object.
>
> That's like saying an integer is inaccessible since it can only be used
> to add/subtract/etc (list of all operations you can do with an
> integer). What does it mean to access something, if not to do some
> operation on it? Getting the referenced object is the operation you can
> do with it.
>
>>>> So in Python, we don't have pointers because we don't have access to
>>>> change or reassign them.
>>>
>>> Yes you do. That's _exactly what happens_ in an assignment statement -
>>> you are reassigning it to the address of another object. And that's
>>> something you *can't do* with references in C++.
>>
>> The operation you describe doesn't change the reference; it doesn't
>> mutate an existing value which can be compared with whatever value it
>> had before. Instead, it throws away one reference and replaces it with a
>> different one.
>
> So, if you have a list x, and assign a new value to x[0], it doesn't
> change the list, because you can't compare the list to the value the
> list had before?

x still refers to a object which in this case happens to be a list. 
You've merely changed the value of the first element of the object that 
is referred to by the name x.

>
> You're not making any sense. It's a value. Changing it and "throwing
> away and replacing with a different one" are the same thing.
>
>> That's significant, because unlike a mutable value you can never again
>> get at the old reference in the Python program.
>
> I don't understand what you mean by "can never again get at" it if you
> think you _can_ do it for mutable values.
>
>>>> You can't, for example, keep the old reference (there are no references
>>>> to references in Python), because they're not accessible as values in
>>>> themselves. Once you assign a different reference, the old one is gone
>>>> and can't be found again.
>>>
>>> You can keep it by copying it to somewhere.
>>
>> How do you propose to “copy” a reference in Python? Making a new
>> reference to the referenced object is not making a copy of the
>> reference.
>
> Yes it is. I don't know why you think it's not, so I can't even figure
> out how to respond to this.
>

No it isn't.  When you make a copy of an object you will end up with two 
names that refer to the same object.

 >>> x = [1,2,3]
 >>> y = x
 >>> x;y
[1, 2, 3]
[1, 2, 3]
 >>> del x
 >>> y
[1, 2, 3]

If y was a copy of x, then when x is blown away how can y still know 
about the list that x originally referred to?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list