I want to use a class instance as a dictionary key, like:
classinstance = class()
dictionary[classinstance] = 'hello world'
Python seems to be not able to handle classes as dictionary key, or am I wrong? In addition, I could use a Tuple-list like [(classinstance, helloworld),...] instead of a dictionary, but that looks very unprofessional. Do you have any clue for fixing that issue?
Your instances need to be hashable. The python glossary tells us:
An object is hashable if it has a hash value which never changes during its lifetime (it needs a
__hash__()
method), and can be compared to other objects (it needs an__eq__()
or__cmp__()
method). Hashable objects which compare equal must have the same hash value.Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().
The following code works well because by default, your class object are hashable :
Class Foo(object):
def __init__(self):
pass
myinstance = Foo()
mydict = {myinstance : 'Hello world'}
print mydict[myinstance]
Output : Hello world
In addition and for more advanced usage, you should read this post :
Try implementing the hash and eq methods in your class.
For instance, here is a simple hashable dictionary class I made:
class hashable_dict:
def __init__(self, d):
self.my_dict = d
self.my_frozenset = frozenset(d.items())
def __getitem__(self, item):
return self.my_dict[item]
def __hash__(self):
return hash(self.my_frozenset)
def __eq__(self, rhs):
return isinstance(rhs, hashable_dict) and self.my_frozenset == rhs.my_frozenset
def __ne__(self, rhs):
return not self == rhs
def __str__(self):
return 'hashable_dict(' + str(self.my_dict) + ')'
def __repr__(self):
return self.__str__()
There is nothing wrong with using an instance as a dictionary key so long as it follows the rules: A dictionary key must be immutable.
来源:https://stackoverflow.com/questions/7560172/can-i-use-an-object-an-instance-of-a-class-as-a-dictionary-key-in-python