(objects as) mutable dictionary keys

Steven Bethard steven.bethard at gmail.com
Fri Jan 14 18:17:53 EST 2005


Peter Maas wrote:
> I have summarized the discussion about the usability of lists (and
> and other mutable types) as dictionary keys and put it into the
> Python wiki.URL: http://www.python.org/moin/DictionaryKeys.

Antoon Pardon wrote:
 > I had a look and I think you should correct the followingr:
 >
 >   Dictionary lookup with mutable types like lists is a source of
 >   unpleasant surprises for the programmer and therefore impossible in
 >   Python.
 >
 > It is not impossible in Python. It may be discouraged but it is not
 > impossible since I have already done so.

John Roth wrote:
 > The last piece has an incorrect conclusion. Lists are not safe
 > _because_ the cmp function is NOT a compare of id(list), but
 > is based on list contents, which can change at any time.
 >
 > It should also be emphasized that the default instance hash
 > and cmp functions quoted make it impossible for two different
 > instances to compare equal, thus there is no reason to store them
 > as dictionary keys: it's simpler to make the value an attribute of
 > the instance and bypass the additional complexity of the dictionary.


Note that Peter Maas has put this up on the Python wiki, so if you find 
things that are wrong or inaccurate, don't hesitate to correct them 
yourselves. =)

That said, I just made a few substantial edits that may have at least 
partially resolved your concerns.

Steve



More information about the Python-list mailing list