(objects as) mutable dictionary keys

Antoon Pardon apardon at forel.vub.ac.be
Mon Jan 17 03:34:59 EST 2005


Op 2005-01-14, Steve Holden schreef <steve at holdenweb.com>:
> Antoon Pardon wrote:
>
>> Op 2005-01-14, Peter Maas schreef <peter at somewhere.com>:
>> 
>>>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.
>>>
>>>This summary might be used as a reference should the 'mutable
>>>dictionary keys' issue come up again in c.l.py.
>>>
>> 
>> 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.
>> 
> Better, perhaps, to say:
>
>    Dictionary lookup with mutable types like lists can be a
>    source of unpleasant surprises for the programmer and
>    therefore not recommended in Python.

IOW Python discourages mutable types in any type of container.

>> It is not impossible in Python. It may be discouraged but it is not
>> impossible since I have already done so.
>> 
> If I discouraged you from shooting yourself in the foot would you do 
> that too?

If I chose not to shoot my self in the foot, it wouldn't be because
you discouraged me. It would be because I heard good arguments.

What are good arguments or bad and how much weight they have depends
on the person and on the circumstances. So a simple rule like:
Never use a mutable as a key in a dictionary will sometimes not be
the best solution.

-- 
Antoon Pardon



More information about the Python-list mailing list