immutability is not strictly the same as having an unchangeable value, it is more subtle

Arup Rakshit ar at zeit.io
Wed Apr 17 05:43:27 EDT 2019


Hi All,


Thanks for explaining it so nice way. I got it now. What protocols I need to learn, to define a custom immutable class ?


Thanks,

Arup Rakshit
ar at zeit.io



> On 16-Apr-2019, at 10:54 PM, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> 
> On Tue, 16 Apr 2019 13:13:18 +0530, Arup Rakshit <ar at zeit.io> declaimed the
> following:
> 
>> 
>> But what I don’t understand here is that what he said about the immutable container objects. Why after changing the value of internal mutable objects we still say the container object as mutable? Any examples to define what the author meant will be helpful.
>> 
> 
> 
> immTuple = ([], {})
> 
> 	A tuple holding a list and a dictionary. You can not change which list
> or which dictionary -- that is fixed (immutable). But the way Python object
> bindings work is that the list and dictionary are not really IN the tuple
> -- the tuple, in the run time level, has references to the list and
> dictionary, with the those objects being stored somewhere else. 
> 
> 	Think of it as the tuple having strings that are tied to the content
> objects. You can not change the strings in the tuple -- they always lead
> from the tuple to the "contained" object. The list and dictionary are also
> containers -- you can add or remove objects (mutation) inside those
> containers, but they remain the same containers.
> 
> 	To really understand this requires understanding Name Binding...
> 
> aName = something
> 
> binds "aName" to the object "something" (it does not copy something to a
> named variable -- unlike many classic languages; in C, FORTRAN, etc. that
> statement copies the contents of the memory address of "something" to the
> memory address of "aName"). Binding is more like writing "aName" on a
> post-it note, attaching that note to a string, and attaching the other end
> of the string to the object "something" (if "something" is another name,
> you follow the string to the actual object, and bind to the object).
> 
> immTuple[0]
> 
> accesses the first element (the list) of the tuple -- you can consider it
> to be a "name" bound to the first element..
> 
> immTuple[0] = anything
> 
> fails because tuples are immutable, and that statement says "change the
> string in the first element of the tuple to connect to a different object"
> 
> immTuple[0].append(anything)
> 
> works because it follows the string /to/ the list object, and that object
> is a container that allows modification of what it contains.
> 
> 
> -- 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
> 	wlfraed at ix.netcom.com 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list