[Tutor] Making Doubly Linked List with Less Lines of Code.

Alex Kleider akleider at sonic.net
Sat Jan 3 07:51:17 CET 2015


On 2015-01-02 13:57, Steven D'Aprano wrote:
> On Fri, Jan 02, 2015 at 09:57:29AM -0800, Alex Kleider wrote:
>> On 2015-01-01 17:35, Alan Gauld wrote:
>> 
>> >Repeats replicates the reference to the object but
>> >does not create a new object.
>> 
>> This part I can understand but, as Steven has pointed out,
>> this behaviour changes if the object being repeated is immutable.
>> Why would one get a new object (rather than a new reference to it)
>> just because it is immutable?  It's this difference in behaviour
>> that depends on mutability that I still don't understand even though
>> Steven did try to explain it to me:
>> """
>> If the list items are immutable, like ints or strings, the difference
>> doesn't matter. You can't modify immutable objects in-place, so you
>> never notice any difference between copying them or not.
>> """
> 
> No, you misunderstood what I was trying to say.

Yes, Steven, I did but you've set that straight and
I thank you for it.  Here's what finally helped me figure
it out:
>>> a = [{}, 999]
>>> b = a*3
>>> b
[{}, 999, {}, 999, {}, 999]
>>> b[2] = 'hello'
>>> b
[{}, 999, 'hello', 999, {}, 999]
>>> b[4]["a"] = 1
>>> b
[{'a': 1}, 999, 'hello', 999, {'a': 1}, 999]
>>> b[4] = 1
>>> b
[{'a': 1}, 999, 'hello', 999, 1, 999]

Again, my compliments to this list for the admirable way it
fulfils its mission!
Alex



> But with immutable objects, there is nothing you can do (except check
> the IDs) that will reveal the difference between having one or two
> distinct objects:
> 
> a = 123
> b = 123
> 
> *may or may not* set a and b to distinct objects. Because when it comes
> to immutable objects, it makes no difference.


More information about the Tutor mailing list