[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Clarification on Immutability please

On 2020-01-21, Chris Angelico <rosuav at> wrote:
> On Wed, Jan 22, 2020 at 8:01 AM Jon Ribbens via Python-list
><python-list at> wrote:
>> On 2020-01-21, Chris Angelico <rosuav at> wrote:
>> > On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker <stephen_tucker at> wrote:
>> >> I am left concluding that mytup is not actually a tuple (even though type
>> >> (mytup) tells me that it is).
>> >
>> > If type(mytup) is tuple, then mytup really truly is a tuple. There is
>> > no other conclusion.
>> >
>> >> My only explanation is that mytup is, actually, a pointer to a
>> >> tuple; the pointer can't change, but the contents of that
>> >> pointer (or the data to which the pointer points) can change.
>> >
>> > Incorrect. Python simply doesn't have that concept.
>> It's a bit weird that you took the bit where he realised exactly what
>> is actually happening and replied "incorrect".
> What do you mean? In what way is mytup a pointer to something that can
> change? If you're looking at a mutable object like a list, then you
> could say that the variable is a reference to something that can
> change ("pointer" is misleading, as Python references don't have
> C-style pointer semantics, but near enough); however, you can't
> replace the object at a particular location with a completely
> different object, you can only make changes that the object itself
> supports. And since this is a tuple, there are no such changes.

Possibly one or both of us is misunderstanding what he meant.
'foo = (1, 2)' binds the name 'foo' as a reference to an immutable
tuple (1, 2). The binding of the name 'foo' is mutable and can be
changed later to bind 'foo' to a different tuple or indeed an object
of a completely different type. The tuple (1, 2) is not mutable.
Whether we call the link between 'foo' and (1, 2) a pointer or a
reference is almost entirely irrelevant, the difference is essentially