When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?
Neil Cerutti
horpner at yahoo.com
Wed Apr 25 09:19:50 EDT 2007
On 2007-04-24, Thomas Nelson <thn at mail.utexas.edu> wrote:
> On Apr 23, 10:38 pm, Mel Wilson <mwil... at the-wire.com> wrote:
>> Even with a balanced tree, if a key in a node changes value,
>> you may have to re-balance the tree. Nothing in a Python list
>> says that a dictionary tree would have to be re-balanced if
>> you changed that particular list.
>
> You don't have to look at any implementation. A dictionary
> maps every key to exactly one object. If the objects were
> mutable, you could change one key to another key, and then
> which item would the dictionary pull?
> (Imaginary code)
> d = {[1,2,3]: 'hat', [1,2,4]: 'sock' }
> for key in d:
> key.pop()
> print d[[1,2]] #does this print hat or sock?
That would be documented as undefined behavior, and users
exhorted not to do such things.
Python's dictionaries are a proven winner--I'm definitely not an
advocate for changing them. But the general requirement for a
mapping container *isn't* that keys be immutable, but that you
either don't mutate keys, or don't do so without also reording
(rehashing?) the mapping.
--
Neil Cerutti
More information about the Python-list
mailing list