在 ZOBD(Python 3.x 中)中,我希望能够将对象作为键存储在 BTrees.OOBTree.OOBTree()
中。 。我尝试时遇到的错误示例(请参阅评论):
from BTrees.OOBTree import OOBTree as Btree
class Test:
pass
bt=Btree()
t=Test()
bt[t]=None #TypeError: Object has default comparison
所以,我在某处读到 __eq__
可能需要定义来消除该错误,但是尽管这似乎解决了前面的问题,但它似乎会导致更多问题。示例:
[编辑:应该注意的是,我发现了继承 OOBTree(和 TreeSet)的一些问题,就像我在这里所做的那样。显然,他们没有正确保存;因此,它与继承 Persistent 不同,即使它们继承了 Persistent。]
from BTrees.OOBTree import OOBTree as Btree
class Test:
def __eq__(self, other): #Maybe this isn't the way to define the method
return self==other
bt=Btree()
t=Test()
bt[t]=None
t in bt #TypeError: unorderable types: Test() < Test()
在 BTree 或 OOBTree 中使用对象作为键的正确方法是什么?我确实需要测试该 key 是否存在。
对于那些不知道的人来说,ZODB 中的 BTree 非常类似于可扩展的 Python 字典(与常规 Python 字典相比,它们应该可以使用更多的键值对)。
最佳答案
关于python - 在 ZOBD OOBTree 中使用对象作为键的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40992268/