我試圖構建一組對象的實例,但是添加某些對象的實例導致TypeError: unhashable instance
。下面是一個小例子:實例集
from sets import Set
import random
from UserDict import DictMixin
class Item1(object):
pass
class Item2(DictMixin):
pass
item_collection = Set()
x = Item1()
y = Item2()
item_collection.add(x) # this works
print item_collection
item_collection.add(y) # this does not
print item_collection
爲什麼失敗,我怎麼能得到一組從DictMixin派生的對象的實例?
使用'id'生成哈希是否有缺點?我在這裏問了一下http://stackoverflow.com/questions/2040101/using-object-id-as-a-hash-for-objects-in-python,因爲這是我想到的新東西。 – 2010-01-11 05:38:45