相关疑难解决方法(0)

实例集

我正在尝试构建一组对象的实例,但是添加某些对象的实例会导致一个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
Run Code Online (Sandbox Code Playgroud)

为什么会失败?如何获取从DictMixin派生的对象的一组实例?

python

1
推荐指数
2
解决办法
2825
查看次数

标签 统计

python ×1