Re: Mutable objects as mapping keys

Kenneth Hinckley (kph2q@bodhi.cs.virginia.edu)
Fri, 26 Mar 93 12:23:11 EST

Guido.van.Rossum@cwi.nl writes:

| My questions to the general public are:

| (1) do you think that disallowing lists as keys is a big drawback?
No, especially since having lists as keys is likely to be (1)
confusing to the programmer and (2) a probable source of bugs or
undesirable interactions with other language features.

| (2) would you accept the performance penalty of always deep copying
| lists used as keys? (2a) when using lists (2b) when not using lists!
(2a) Yes
(2b) Absolutely not! Using lists as keys is probably handy about one
time out of a hundred, and the other 99 times you shouldn't have to
pay for the cost.

| (3) do you happen to have another implementation idea?
You might consider having an immutable list type, i.e. a list that
doesn't support .append(), etc. Of course, now you have the problem
of specifying whether a given list is mutable or immutable; python's
aversion to static type declarations might make this idea impractical.

Perhaps another way to approach it would be to have a .lock() method
for lists. When a list is 'locked', it is immutable-- trying to call
a method that changes the list would raise an exception. Trying to
place an unlocked list in a dictionary would also raise an exception.

Hope some of this is vaguely useful.

Ken