lambda

Nick Coghlan ncoghlan at iinet.net.au
Tue Jan 18 07:42:21 EST 2005


Antoon Pardon wrote:
> More specific the Decimal class is mutable and usable as dict key.

It's *meant* to be immutable though. The fact that we used __slots__ instead of 
__setattr__ to implement the immutability, so you *can* overwrite the slot 
variables if you really want to is merely an artifact of the current Python 
implementation.

The limited mutability bug will disappear in Python 2.5, so it's not a good 
example of a 'mutable' dict key (especially given that the only way to mutate it 
is to modify private variables directly).

And, as I've stated previously, if the issue of sane mutable keys in 
dictionaries and sets really bugs you so much - implement identity_dict and 
identity_set in C and lobby for their inclusion in the collections module.

On the more general point of "don't use mutable objects with non-identity based 
comparisons as dictionary keys", try teaching students for a while (or listen to 
those who have):

When stating useful general principles, it is never, ever worth it to get into 
the quibbly little details about exceptions to the principle. If students ask, 
admit that they exist, but point out that the exceptions are rare, and not worth 
worrying about at that point in their learning.

Only when one is aware of the reasons for a principle, can one be aware of good 
reasons not to follow it :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list