On Sun, 08 Apr 2007 22:20:45 -0700, James Stroud wrote: > Steven D'Aprano wrote: >> On Mon, 09 Apr 2007 02:26:37 +0000, James Stroud wrote: >> >>> Bart Willems wrote: >>>> James Stroud wrote: >>>>> ... It boils down to the fact that tuples are useless as a result >>>>> unless you know you really need them--and you never really NEED them. >>>> Could you clarify that for me? I use tuples *a lot* and I really *NEED* >>>> them - I'm building a lot of multi-tier reports where detail-level data >>>> is pulled out of a dictionary based on a composed key. It is impossible >>>> to build those dictionaries *without* using tuples. >>> >>> "Impossible" is a strong word, as is "need" (especially when in all caps). >>> >>> py> import md5 >>> py> class HashedList(list): >>> ... def __hash__(self): >>> ... h = md5.new() >>> ... for item in self: >>> ... h.update(str(hash(item))) >>> ... return int(h.hexdigest(), 16) >>> ... >>> py> hl = HashedList('bob', 'carol', 'ted') >>> py> {hl:3} >>> {['bob', 'carol', 'ted']: 3} >>> >>> Impossible? I wouldn't even say that this was all that difficult. >> >> Possible, if by possible you mean "broken". >> >> >>>>> D = {hl: 3} >>>>> D >> {['bob', 'carol', 'ted']: 3} >>>>> hl[0] = 'Bob' >>>>> D >> {['Bob', 'carol', 'ted']: 3} >>>>> D.keys()[0] is hl >> True >>>>> D[hl] >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> KeyError: ['Bob', 'carol', 'ted'] >> >> > > def __setitem__(self, *args): > raise TypeError, '%s doesn't support item assignment.' % > self.__class__.__name__ > > > Problem fixed. Next?
hl.reverse() hl.sort() # if the list isn't already sorted del hl[0] hl.append() etc. Yes, you can block those as well... but by the time you've finished making your HashedList immutable, it is just a slower tuple with a different name. In other words... you can avoid using tuples by using a tuple with a different name. You might as well just do this: HashedList = tuple -- Steven. -- http://mail.python.org/mailman/listinfo/python-list