When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

Steve Holden steve at holdenweb.com
Wed Apr 25 09:42:17 EDT 2007


Neil Cerutti wrote:
> 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.
> 
And which API do you use to "reord(er?) (rehash) the mapping"?

regards
  Steve
-- 
Steve Holden       +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list