On Tue, May 15, 2012 at 3:25 AM, Christian Heimes <li...@cheimes.de> wrote: > Code explains more than words. I've created two examples that some issues. > > Mutable values break dicts as you won't be able to retrieve the same > object again:
Sure, you'll get no argument from me on that. I was more interested in the other one. > When you mess up __eq__ you'll get funny results and suddenly the > insertion order does unexpected things to you: > >>>> class Example2(object): > ... def __init__(self, value): > ... self.value = value > ... def __hash__(self): > ... return hash(self.value) > ... def __eq__(self, other): > ... return hash(self) == hash(other) > ... >>>> d = {} >>>> ob = Example2("egg") >>>> d[ob] = True >>>> d > {<__main__.Example2 object at 0x7fab66cb7610>: True} >>>> d["egg"] = True >>>> d > {<__main__.Example2 object at 0x7fab66cb7610>: True} >>>> d2 = {} >>>> d2["egg"] = True >>>> d2[ob] = True >>>> d2 > {'egg': True} That's just how sets and dicts work with distinct objects that compare equal. I don't see any fundamental difference between that and this: >>> d = {} >>> d[42] = True >>> d {42: True} >>> d[42.0] = True >>> d {42: True} >>> d2 = {} >>> d2[42.0] = True >>> d2 {42.0: True} >>> d2[42] = True >>> d2 {42.0: True} I wouldn't consider the hashing of ints and floats to be broken. -- http://mail.python.org/mailman/listinfo/python-list