tuples are useless???
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Sun Apr 8 23:30:29 EDT 2007
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']
--
Steven.
More information about the Python-list
mailing list