Why is dictionary.keys() a list and not a set?

Mike Meyer mwm at mired.org
Thu Nov 24 13:32:40 EST 2005


Christoph Zwerschke <cito at online.de> writes:

>>>- Because sets can only contain immutable values
>
> Mike Meyer wrote:
>
>> Not true. Sets can only contain *hashable* objects, which isn't the
>> same thing.
>
> I trusted the doco which defines a set as "an unordered collection of
> immutable values" (http://docs.python.org/lib/types-set.html).

If the hash changes, you've screwed up the set. What it really should
say is "collection of objects with fixed hashes", or words to that
effect. Do you want to file a PR on this?

> Anyway, the problems stays the same.

How so? As I demonstrated, you can subclass any class that doesn't
have a hash to add one, and then use the subclass, which - except for
having a hash - will have exactly the same behavior as your original
class.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list