Pre-PEP: __hashable__
Duncan Booth
duncan at NOSPAMrcp.co.uk
Wed Dec 11 05:05:11 EST 2002
Chad Netzer <cnetzer at mail.arc.nasa.gov> wrote in
news:mailman.1039581793.4260.python-list at python.org:
> Not all tuples are hashable. Actually, that clears up one final
> point of discussion I was having with Tim Delaney. Tuples are NOT
> replacements for his immutablelists, since his immutablelists ARE
> meant to always be hashable (I assume, or else they cannot be
> created...)
>
This means that the __hashable__ method has to be recursive, and quite
probably has to take steps to avoid infinite recursion as with deepcopy:
aList = [ {'a': 1} ]
Ok, aList.__hashable__ has to call hashable on each of its elements.
bList = [ 1, 2, 3 ]
blist[1] = blist
At this point bList.__hashable__ had better be maintaining some sort of
state dictionary to avoid the infinite recursion. Perhaps in this simple
case it would be alright just to give up and throw an exception when the
recursion limit is reached, but I think it wouldn't be too hard to produce
a data structure that explodes its memory usage unreasonably unless you do
the same sort of checking as for deepcopy. e.g.
cList = [ somelargishdictionary ] * 2000
cList[2000] = cList
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list