News for hackers. Stuff that matters.
Re:Python Lists vs. Tuples by kazinator on Friday August 19, 2016 @09:35PM (#12324631)
You mean in Python or in computer science?

Because I'm pretty sure that it's possible to use mutable objects as keys in general. The users just have to observe restrictions: like don't insert the keys into the dictionary and then mutate them while they are in the dictionary and expect the dictionary to still work fine.

(Objects can also be used as keys even if they are mutated while in the dictionary, if identity-based equality is used for the dictionary; i.e. two keys are equal if they are acually one and the same object, and not just two similar objects.)

Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:36PM (#12324865)
> Because I'm pretty sure that it's possible to use mutable objects as keys in general. The users just have to observe restrictions: like don't insert the keys into the dictionary and then mutate them while they are in the dictionary and expect the dictionary to still work fine.

In other words, mutable objects don't work as keys, and pretending they do requires unenforced conventions to preserve the integrity of the dictionary abstraction.

> Objects can also be used as keys even if they are mutated while in the dictionary, if identity-based equality is used for the dictionary; i.e. two keys are equal if they are actually one and the same object, and not just two similar objects.

The fundamental problem is very simple: dictionary keys have to be values, not objects, and Python simply doesn't have compound values. There is no such thing as “the list [1,2,3]” in Python: you just have one or more objects whose current state happens to be the sequence 1,2,3. The very next moment, their states might be different sequences. Object identities are first-class values in Python, but identities are primitive and indecomposable - not compound!

Re:Python Lists vs. Tuples by kilink on Friday August 19, 2016 @10:11PM (#12324756)
This is a Python specific restriction. Ruby for example allows mutable keys in dictionaries.
Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:20PM (#12324799)
So, which is the case?

(0) Object identities are used as keys? (e.g., two different list objects that contain the elements 1,2,3 are treated as different keys)

(1) Catastr... I mean, hilarious things happen if you mutate an object that's being used as a key?

Re:Python Lists vs. Tuples by kilink on Friday August 19, 2016 @10:40PM (#12324888)
I'm not sure if you are asking about Python, or Ruby.

Anyway, both languages use value-based hashing for dictionaries. I am not a Rubyist, but I guess you are just expected to not mutate the keys? Ruby's dictionary implementation also has a rehash method to deal with this case [1], if for some reason you are purposely mutating the keys I suppose...

[1] http://docs.ruby-lang.org/en/2.0.0/Hash.html#method-i-rehash

Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:56PM (#12324948)
> I'm not sure if you are asking about Python, or Ruby.

Ruby, since you said it allows mutable keys in dictionaries.

> Anyway, both languages use value-based hashing for dictionaries.

Neither Python nor Ruby has compound values. Object identities are primitive values.

> but I guess you are just expected to not mutate the keys?

I guess C programmers are expected not to use memory after they free it.

Re:Python Lists vs. Tuples by kazinator on Friday August 19, 2016 @10:24PM (#12324810)
But if Python got rid of tuples, it would be a radical change; in that case it could as well allow lists to be dictionary keys in the same stroke.