Skip to content

Module 4 · Operators — topic 14 of 16

Identity operators

is and is not test whether two names refer to the very same object, which is different from having equal values. Learn when to use is (None, sentinels) and when to use ==.

Intermediate 10 min read·Lesson 57 of 306 in Python Programming

Learning objectives

  • Distinguish identity (is) from equality (==)
  • Use is None correctly
  • Avoid is for comparing numbers and strings

Definition

is and is not test whether two names refer to the very same object, which is different from having equal values. Learn when to use is (None, sentinels) and when to use ==.

Introduction

Two values can be equal without being the same object. Two separate lists [1, 2] and [1, 2] are equal (==) but are two different objects in memory. The identity operators is and is not ask whether two names point to exactly the same object.

Why it matters

Using is where == is meant causes bugs that appear only sometimes, which are the hardest to track down. Knowing the difference, and the few places where is is right, such as checking for None, keeps comparisons correct.

Detailed explanation

a == b compares values: do they have the same contents?

a is b compares identity: are they the same object? It is equivalent to id(a) == id(b).

Assigning b = a makes a is b True, because both names refer to one object.

Creating two lists separately makes them equal but not identical.

Use is for singletons, values of which only one exists: None, True and False. The idiom is if value is None.

Don’t use is to compare numbers or strings. Python sometimes reuses small integers and short strings, so is may appear to work in a quick test and then fail with other values. Recent Python versions even warn: SyntaxWarning: "is" with a literal.

Syntax

Text
if value is None:
if a is b:
if result is not None:
  • is checks for the same object.
  • is not is its negation.
  • Use == for comparing values.

Code examples

Equal but not identical

Python
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b, a is b)
print(a == c, a is c)
Output
True False
True True

Checking for None

Python
result = None
print(result is None)
result = "done"
print(result is not None)
Output
True
True

More examples

Why identity matters for mutable objects

Python
default_settings = {"theme": "light"}
user_a = default_settings
user_b = dict(default_settings)
user_a["theme"] = "dark"
print("same object as defaults?", user_a is default_settings, user_b is default_settings)
print("defaults now:", default_settings)
print("user_b:", user_b)
print("ids equal:", id(user_a) == id(default_settings))
Output
same object as defaults? True False
defaults now: {'theme': 'dark'}
user_b: {'theme': 'light'}
ids equal: True

Step-by-step explanation

  • user_a = default_settings shares the same dictionary object.
  • dict(default_settings) creates a new, separate dictionary with the same contents.
  • Changing user_a also changes default_settings, because they are the same object (is is True).
  • user_b keeps the original theme, and id() confirms user_a and the defaults share one identity.

Expected output

Running “Why identity matters for mutable objects” prints exactly:

TextOutput
same object as defaults? True False
defaults now: {'theme': 'dark'}
user_b: {'theme': 'light'}
ids equal: True

Practical applications

  • Library code checks optional arguments with if timeout is None.
  • Caching code uses is to check whether it already holds the exact object.
  • Debugging aliasing bugs often starts with printing id() of two variables.

Best practices

  • Use == for values, is only for None, True, False and deliberate identity checks.
  • Use is not None rather than not value when 0 or "" are valid.
  • Copy mutable objects when you need independence.

Common mistakes

Using is to compare values

Avoid

if name is "admin":

Prefer

if name == "admin":

Two equal strings may be different objects; is would then be False.

Using == None

Avoid

if value == None:

Prefer

if value is None:

is None is the idiomatic, exact test for the single None object.

Key takeaways

  • is tests whether two names refer to the same object.
  • == tests whether values are equal.
  • Use is with None; use == for numbers and strings.

Try it yourself

  1. 1
    Create two equal tuples separately and print both == and is for them. Explain the results.
  2. 2
    Write a function with a parameter that defaults to None and prints whether it was given.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic