列表和集合的成员资格测试有何不同?

发布于 2024-10-20 13:55:32 字数 780 浏览 2 评论 0原文

我无法弄清楚为什么第一个断言没问题,而第二个断言却引发错误。

subject_list = [Subject("A"), Subject("B"), Subject("C")]
subject_set = set()
subject_set.add(Subject("A"))
subject_set.add(Subject("B"))
subject_set.add(Subject("C"))

self.assertIn(Subject("A"), subject_list)
self.assertIn(Subject("A"), subject_set)

错误如下:

Traceback (most recent call last):
  File "C:\Users\...\testSubject.py", line 34, in testIn
    self.assertIn(Subject("A"), subject_set)
AssertionError: <Subject: A> not found in set([<Subject: B>, <Subject: C>, <Subject: A>])

Subject 类中的相等性测试只是 self.name == other.name,而在另一个 UnitTest 中,我验证 Subject("A") == subject (“A”)。我真的不明白为什么该主题在列表中而不是在集合中。理想情况下,我希望主题同时存在于两者中。

I'm having trouble with figuring out why the first of these assertions is OK and the second raises an error.

subject_list = [Subject("A"), Subject("B"), Subject("C")]
subject_set = set()
subject_set.add(Subject("A"))
subject_set.add(Subject("B"))
subject_set.add(Subject("C"))

self.assertIn(Subject("A"), subject_list)
self.assertIn(Subject("A"), subject_set)

Here is the error:

Traceback (most recent call last):
  File "C:\Users\...\testSubject.py", line 34, in testIn
    self.assertIn(Subject("A"), subject_set)
AssertionError: <Subject: A> not found in set([<Subject: B>, <Subject: C>, <Subject: A>])

The test for equality in the Subject class is simply self.name == other.name, and in another UnitTest I verify that Subject("A") == Subject("A") . I really can't figure out why the subject is in the list and not in the set. Ideally I'd like the subject to be in both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能怎样 2024-10-27 13:55:32

该表达式

Subject("A") in subject_list

将使用 Subject.__eq__() 方法将 Subject("A")subject_list 中的每个条目进行比较。如果不覆盖此方法,则默认情况下始终返回 False,除非两个操作数是同一对象。如果 Subject 缺少 __eq__() 方法,上面的表达式将始终返回 False,因为 Subject("A") > 是一个新实例,不能已在列表中。

相反的表达式

Subject("A") in subject_set

将首先使用 Subject.__hash__() 来查找正确的存储桶,然后才使用 Subject.__eq__() 。如果您没有以与 Subject.__eq__() 兼容的方式定义 Subject.__hash__(),则此操作将会失败。

The expression

Subject("A") in subject_list

will compare Subject("A") to each entry in subject_list using the Subject.__eq__() method. If this method is not overwritten, it defaults to always return False unless the two operands are the same object. The above expression would always return False if Subject lacked a __eq__() method, since Subject("A") is a new instance which cannot already be in the list.

The expression

Subject("A") in subject_set

on the contrary will use Subject.__hash__() first to find the right bucket, and use Subject.__eq__() only after this. If you did not define Subject.__hash__() in a way compatible with Subject.__eq__(), this will fail.

蓝海 2024-10-27 13:55:32

集合中的成员资格还取决于对象的哈希,因此您必须在类上适当地实现 __hash__() 方法。

Membership in a set also depends on the object's hash, and as such you must implement the __hash__() method on the class appropriately.

鹿港巷口少年归 2024-10-27 13:55:32

要么您的 Subject 类中没有 __hash__() 方法,要么它是不可靠的。试试这个:

def __hash__(self):
    return hash(self.name)

文档位于此处

Either you don't have a __hash__() method in your Subject class, or it is dodgy. Try this:

def __hash__(self):
    return hash(self.name)

The docs are here.

缘字诀 2024-10-27 13:55:32

要在集合中使用它们,您必须确保 Subject 可以正确散列。如果您自己没有定义__hash__,它只会采用id,并且对于不同的实例来说这是不同的。 __hash__ 的定义应使相同的对象具有相同的哈希值。

To use these in a set, you have to make sure Subject is properly hashable. If you do not define __hash__ yourself, it will simply take the id, and that is different for different instances. __hash__ should be defined such that equal objects have equal hashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文