Steven D'Aprano wrote: > 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...
To prevent abuse. but by the time you've finished making > your HashedList immutable, it is just a slower tuple with a different > name. But has an index() method. > 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 You can avoid using tuples any number of ways. For example, you can catenate strings. You seem to be under the misapprehension that I authored the Subjet line. Please read the original quote: > 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. Bear in mind that this was also a response to someone admittedly less familiar to the language--to encourage him to use lists in favor of tuples to avoid inconveniences later. The HashedList is to show that you can always get around the requirement for a tuple and thus an absolute need for a tuple doesn't exist, as implied by the quote. I make no assertions as to the HashedLists efficiency. I use tuples all of the time, but I could just as well catenate strings etc. They are a language convenience that are ultimately dispensible. James -- http://mail.python.org/mailman/listinfo/python-list