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 ==.
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
- is checks for the same object.
- is not is its negation.
- Use == for comparing values.
Code examples
Equal but not identical
True False True True
Checking for None
True True
More examples
Why identity matters for mutable objects
same object as defaults? True False
defaults now: {'theme': 'dark'}
user_b: {'theme': 'light'}
ids equal: TrueStep-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:
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
Prefer
Two equal strings may be different objects; is would then be False.
Using == None
Avoid
Prefer
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
- 1Create two equal tuples separately and print both == and is for them. Explain the results.
- 2Write 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?