assertEqual 和 assertIs 之间有什么区别(assertIs 是在 Python 2.7 中引入的)? [英] What's the difference between assertEqual and assertIs (assertIs was introduced in Python 2.7)?

查看:45
本文介绍了assertEqual 和 assertIs 之间有什么区别(assertIs 是在 Python 2.7 中引入的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 - http://docs.python.org/library/unittest.html#assert-方法

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

推荐答案

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

Using assertEqual the two objects need not be of the same type, they merely need to be the same value. In comparison, using assertIs the two objects need to be the same object.

assertEqual 测试是否相等,如 == 运算符:

assertEqual tests for equality like the == operator:

运算符 <、>、==、>=、<= 和 != 比较两个对象的值.对象不必具有相同的类型.如果两者都是数字,则将它们转换为公共类型.否则,不同类型的对象总是比较不相等,并且顺序一致但任意.

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 运算符相同:

assertIs test for object identity same as the is and is not operators:

运算符是和不是测试对象身份:x is y 当且仅当 x 和 y 是同一个对象时才为真.x 不是 y 产生逆真值.

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 比较.

The above quotes both come from the Python documentation section 5.9 Comparisons.

这篇关于assertEqual 和 assertIs 之间有什么区别(assertIs 是在 Python 2.7 中引入的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆