We use identity checks to see if two things are exactly the same object in memory, not just equal in value.
When you want to confirm two variables point to the exact same object.
When testing if a function returns a singleton like None or True.
When checking if two references are not the same object.
When you want to avoid confusing objects that look equal but are different.
When verifying caching or memoization returns the same object.
Syntax
PyTest
assert a is b
assert a isnot b
is checks if two variables refer to the same object.
is not checks if two variables do not refer to the same object.
Examples
Both variables point to the same None object, so the test passes.
PyTest
a = None
b = Noneassert a is b
Both variables refer to the same list object.
PyTest
a = [1, 2]
b = a
assert a is b
Lists have the same content but are different objects, so the test passes.
PyTest
a = [1, 2]
b = [1, 2]
assert a isnot b
Sample Program
This test checks identity using is and is not. It shows when objects are the same or different.
PyTest
import pytest
def test_identity_checks():
x = None
y = Noneassert x is y # Both None, same object
a = [1, 2, 3]
b = a
assert a is b # Same list object
c = [1, 2, 3]
assert a isnot c # Different list objects
d = True
e = Trueassert d is e # True is a singleton
f = 1000
g = 1000assert f isnot g # Large integers may be different objectsif __name__ == '__main__':
pytest.main([__file__])
OutputSuccess
Important Notes
Use is only to check identity, not equality of values.
For value equality, use == instead.
Singletons like None, True, and False are good examples for identity checks.
Summary
is checks if two variables point to the same object.
is not checks if two variables point to different objects.
Use identity checks to confirm exact object sameness, not just equal values.
Practice
(1/5)
1. What does the is operator check in Python when used in pytest assertions?
easy
A. If two variables have the same value but different objects
B. If two variables point to the exact same object in memory
C. If two variables are equal using the == operator
D. If two variables are both integers
Solution
Step 1: Understand the is operator meaning
The is operator checks if two variables refer to the same object in memory, not just equal values.
Step 2: Differentiate from ==
The == operator checks if values are equal, but is checks identity (same object).
Final Answer:
If two variables point to the exact same object in memory -> Option B
Quick Check:
is checks identity [OK]
Hint: Remember: is means same object, not just equal value [OK]
Common Mistakes:
Confusing is with ==
Thinking is checks value equality
Using is for comparing numbers or strings incorrectly
2. Which of the following is the correct syntax to assert two variables a and b are not the same object in pytest?
easy
A. assert a is not b
B. assert a !== b
C. assert a is! b
D. assert a != b
Solution
Step 1: Recall correct syntax for identity negation
The correct syntax to check two variables are not the same object is is not, so assert a is not b.
Step 2: Identify invalid syntax options
Options B and C use invalid Python syntax. assert a != b uses != which checks value inequality, not identity.
Final Answer:
assert a is not b -> Option A
Quick Check:
is not syntax correct [OK]
Hint: Use is not to check different objects, not != [OK]
Common Mistakes:
Using != instead of is not
Writing is! which is invalid
Using JavaScript style !== in Python
3. Given the code:
obj1 = None
obj2 = None
assert obj1 is obj2
What will happen when this pytest assertion runs?
medium
A. SyntaxError due to wrong assertion
B. The assertion fails because None is not an object
C. The assertion passes because both are the same None object
D. The assertion fails because obj1 and obj2 are different None instances
Solution
Step 1: Understand None is a singleton
In Python, None is a singleton object, so all variables assigned None point to the same object.
Step 2: Check identity assertion
The assertion assert obj1 is obj2 checks if both variables point to the same object, which they do.
Final Answer:
The assertion passes because both are the same None object -> Option C
Quick Check:
None is None always True [OK]
Hint: Remember: None is always the same object in Python [OK]
A. RuntimeError because lists cannot be compared with is
B. No error; assertion passes because lists have same values
C. SyntaxError due to wrong use of is
D. Assertion fails because lists are different objects despite same values
Solution
Step 1: Understand list object identity
Two lists with same values are different objects in memory unless explicitly assigned.
Step 2: Check assertion behavior
The assertion assert list1 is list2 fails because is checks identity, not equality.
Final Answer:
Assertion fails because lists are different objects despite same values -> Option D
Quick Check:
Same values ≠ same object [OK]
Hint: Use == for value equality, is for identity [OK]
Common Mistakes:
Expecting is to check value equality
Confusing object identity with content equality
Assuming lists with same content share identity
5. You want to write a pytest test to confirm that a function returns the exact same singleton object config every time it is called. Which assertion is best?
hard
A. assert get_config() is config
B. assert get_config() == config
C. assert get_config() is not config
D. assert get_config() != config
Solution
Step 1: Understand singleton identity check
To confirm the function returns the exact same object, use is to check identity.
Step 2: Evaluate options
assert get_config() == config checks value equality, which may be true for different objects. assert get_config() is config correctly asserts identity. Options C and D check for difference, which is not desired.
Final Answer:
assert get_config() is config -> Option A
Quick Check:
is for singleton identity [OK]
Hint: Use is to confirm same singleton object returned [OK]
Common Mistakes:
Using == which checks value, not identity
Using is not by mistake
Not understanding singleton pattern in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.