<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.3105.105" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>From: "Aahz Maruch" <<A
href="mailto:aahz@panix.com">aahz@panix.com</A>><BR>Newsgroups:
comp.lang.python<BR>To: <<A
href="mailto:python-list@python.org">python-list@python.org</A>><BR>Sent:
Wednesday, March 28, 2001 11:35 AM<BR>Subject: Re: list vs tuple<BR><BR><BR>>
In article <<A
href="mailto:buqw6.1999$sk3.675030@newsb.telia.net">buqw6.1999$sk3.675030@newsb.telia.net</A>>,<BR>>
Fredrik Lundh <<A href="mailto:fredrik@effbot.org">fredrik@effbot.org</A>>
wrote:<BR>> >Mark Pilgrim wrote<BR>> >><BR>> >> It was
my understanding that only tuples containing immutable objects<BR>> >>
could be used as dictionary keys. For instance, (1,2,3) can be,
and<BR>> >> ("a","b","c") can be, but ([1,2,3], ["a","b","c"]) can not,
because<BR>> >> the underlying objects could mutate and cause
problems. (Wow, that<BR>> >> sounds like a bad sci-fi
movie.) Is this true?<BR>> ><BR>> >no python interpreter
within reach today?<BR>> ><BR>> >>>> d = {}<BR>>
>>>> d[(1, 2, 3)] = 1<BR>> >>>> d[("a", "b", "c")] =
2<BR>> >>>> d[([1, 2, 3], ["a", "b", "c"])] = 3<BR>>
>Traceback (most recent call last):<BR>> > File "<stdin>",
line 1, in ?<BR>> >TypeError: unhashable type<BR>><BR>> Sure, but
someone could create a class with __hash__ and do some weird<BR>> stuff that
way. Not Recommended.<BR>> --<BR><BR>Actually it's not that bad if you
follow the rules. You should make sure<BR>that __hash__ only uses private
members (starting with "__") that are only<BR>modified in __init__. You
also have to overload __cmp__ so that it also<BR>depends on the same members as
__hash__ (i.e. hashes should be equal if<BR>instances are equal). Or you
can make __hash__ return id(self) if all<BR>instances are considered
unique.<BR><BR>If you mess up, only the offending items will become inaccessible
in your<BR>dictionary. Nightmares of the hash table becoming completely
corrupt are<BR>exagerated. Cross-links won't happen since a compare will
verify the hash<BR>hit, so bugs won't get overly nasty. The following will
safely check the<BR>integrity of a dictionary (and raise a KeyError on damaged
hash values):<BR><BR>for k in d.keys(): d[k]<BR><BR>- Ken Seehof<BR><A
href="mailto:kseehof@neuralintegrator.com">kseehof@neuralintegrator.com</A><BR><A
href="http://www.neuralintegrator.com">www.neuralintegrator.com</A><BR><BR><BR></FONT></DIV></BODY></HTML>