Antoon Pardon wrote: > Op 2005-01-10, Bruno Desthuilliers schreef <[EMAIL PROTECTED]>: >> Antoon Pardon a écrit : >>> Op 2005-01-08, Bruno Desthuilliers schreef <[EMAIL PROTECTED]>: >>> >>>>worzel a écrit : >>>> >>>>>I get what the difference is between a tuple and a list, but why would I >>>>>ever care about the tuple's immuutability? >>>> >>>>Because, from a purely pratical POV, only an immutable object can be >>>>used as kay in a dict. >> >><my-bad> s/kay/key/ </my-bad> >> >>> This is not true. >> >> Chapter and verse, please ? > > I don't need chapter and verse. I have already used mutable > objects as keys and it works just fine. > >>>> class hlst(list): >>>> >>>> def __hash__(self): >>>> sum = 0 >>>> for el in self: >>>> sum += hash(el) >>>> return sum % 0x37777777 >>>>
Given this hash function, how do you handle changed keys? .class hlst(list): . def __hash__(self): . sum = 0 . for el in self: . sum += hash(el) . return sum % 0x37777777 . .lst = hlst([1,2,3]) . .d = {} .d[lst] = 1 . .lst[0] = 0 . .print d .try: . print d[hlst([0,2,3])] .except KeyError: . print "0,2,3: KeyError" .try: . print d[hlst([1,2,3])] .except KeyError: . print "1,2,3: KeyError" raises the KeyError twice. How do you access the element then? And if you can't access the element when it's changed, what is the advantage over using tuples? Reinhold -- http://mail.python.org/mailman/listinfo/python-list