gpt4 book ai didi

python - assertEqual 和 assertIs 有什么区别(assertIs 是在 Python 2.7 中引入的)?

转载 作者:太空狗 更新时间:2023-10-29 21:20:20 25 4
gpt4 key购买 nike

引用 - http://docs.python.org/library/unittest.html#assert-methods

assertEqual(a, b)   # checks that a == b
assertIs(a, b) # checks that a is b <---- whatever that means????

最佳答案

使用assertEqual 两个对象不需要是同一类型,它们只需要是相同的值。相比之下,使用 assertIs 两个对象需要是同一个对象。

assertEqual== 运算符一样测试相等性:

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

assertIs 测试对象身份,与 isis not 运算符相同:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

以上引述均来自 Python 文档部分 5.9 Comparisons .

关于python - assertEqual 和 assertIs 有什么区别(assertIs 是在 Python 2.7 中引入的)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7281774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com