Checking the Identity of Objects
In this lesson, you’ll see a best practice for using
not when checking for object identity.
Every object in Python has a unique identifying number which can be found using the
id() function. If two names have the same
id() value, then they are both referring to the same object.
In Python you use the
is operator to see if two names are really representing the same object. For example, if you create a variable,
obj, then assign it to
is operator will tell you that
obj really is
It’s another name for
None. If you want to test if two names represent different objects, you use
not much in the same way you saw in the last lesson.
It’s preferred to check
obj is not None than to type
not obj is None.
01:04 Next you’ll look at best practices for avoiding unnecessary negative logic.
Become a Member to join the conversation.