The Python Apprentice
上QQ阅读APP看书,第一时间看更新

Testing for equality of identity with is

In reality, the id() function is seldom used in production Python code. Its main use is in object model tutorials (such as this one!) and as a debugging tool. Much more commonly used than the id() function is the is operator which tests for equality of identity. That is, is tests whether two references refer to the same object:

>>> a is b
True

We've already met the is operator earlier, in Chapter 1, Getting started, when we tested for None:

>>> a is None
False

It's critical to remember that is is always testing identity equality, that is, whether two references refer to the exact same object. We'll look in-depth at the other primary type of equality, value equality, in just a bit.