Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # wrt http://stackoverflow.com/q/35826534/1431750
- >>> class Test2(object):
- ... def __init__(self, name):
- ... self.name = name
- ...
- ... def __eq__(self, other):
- ... return self.name == other.name
- ...
- >>> test_Dict = {}
- >>> test_List = []
- >>>
- >>> obj1 = Test2('a')
- >>> obj2 = Test2('a')
- >>>
- >>> test_Dict[obj1] = 'x'
- >>> test_Dict[obj2] = 'y'
- >>>
- >>> test_List.append(obj1)
- >>> test_List.append(obj2)
- >>>
- >>> test_Dict
- {<__main__.Test2 object at 0x0000000002EFC518>: 'x', <__main__.Test2 object at 0x0000000002EFC940>: 'y'}
- >>> test_List
- [<__main__.Test2 object at 0x0000000002EFC518>, <__main__.Test2 object at 0x0000000002EFC940>]
- >>>
- >>> Test2('a') in test_Dict
- False
- >>> Test2('a') in test_List
- True
- >>> # so the `in` test calls `__eq__` for lists but not for dicts
- ... # but not having a __hash__ doesn't return None, doesn't throw an error and doesn't make it "unhashable"
- ... # ...in Python2 if it matters
- ...
- >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement