[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: objects as mutable dictionary keys
From: Andrew Dalke <dalke () dalkescientific ! com>
Date: 2004-12-27 22:59:14
Message-ID: pan.2004.12.27.22.59.45.627552 () dalkescientific ! com
[Download RAW message or body]
Andrew Koenig:
> If d is a dict and t1 and t2 are tuples, and t1 == t2, then d[t1] and d[t2]
> are the same element.
So long as the elements of t1 and t2 are well-behaved.
>>> class Spam:
... def __hash__(self):
... return id(self)
... def __eq__(self, other):
... return True
...
>>> t1 = (Spam(),)
>>> t2 = (Spam(),)
>>> t1 == t2
True
>>> hash(t1) == hash(t2)
False
>>> d = {t1: "T1", t2: "T2"}
>>> d[t1]
'T1'
>>> d[t2]
'T2'
>>> d[t1] == d[t2]
False
>>> d[t1] is d[t2]
False
>>>
Dictionaries have the extra requirement that if
k1 == k2 then hash(k1) == hash(k2).
Andrew
dalke@dalkescientific.com
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic