I am using Python2.7 to write Set
Data Struction. But I got confused to when using Dict.get
. Here is my code:
#!/usr/bin/env python
# -*- coding:UTF-8
__author__ = 'shenshijun'
class Node(object):
def __init__(self, key, weight):
"""Constructor for """
self.key = key
self.weight = weight
def __cmp__(self, other):
return cmp(self.key, other.key)
def __str__(self):
return "".join(['Node(key=', str(self.key), ',weight=', str(self.weight), ")"])
__dict = {}
a1 = Node('A', 1)
a2 = Node('A', 2)
__dict[a1] = a1
print a1 == a2
print(__dict.get(a2))
the output of this code is the following:
True
None
So I guess that Python use is
operator to compare the keys when searching. Can someone one find the truth?
The Python I used:
2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
I have just solve my problem, but any suggestions about how to implements __hash__
in this situation?