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 >>> >>> lst = hlst([3,5,7]) >>> lst [3, 5, 7] >>> lst[0] = 12 >>> lst [12, 5, 7] >>> d = {} >>> d[lst] = 4 >> >>>So you can use tuples for 'composed key'. >> >> lists can be so used too. Just provide a hash. > > Please show us an example, and let's see how useful and handy this is > from a "purely practical POV" ?-) It is handy from a pratical point of view when most operations you do on your data are the equivalent of appending, deleting and changing one element. Simulating these with tuples will cost you more than putting a copy of your list in the dictionary as a precaution against accidently mutating a key. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list