Bird
0
0

Given the code:

medium📝 Predict Output Q13 of 15
PyTest - Writing Assertions
Given the code:
obj1 = None
obj2 = None

assert obj1 is obj2

What will happen when this pytest assertion runs?
ASyntaxError due to wrong assertion
BThe assertion fails because None is not an object
CThe assertion passes because both are the same None object
DThe assertion fails because obj1 and obj2 are different None instances
Step-by-Step Solution
Solution:
  1. Step 1: Understand None is a singleton

    In Python, None is a singleton object, so all variables assigned None point to the same object.
  2. Step 2: Check identity assertion

    The assertion assert obj1 is obj2 checks if both variables point to the same object, which they do.
  3. Final Answer:

    The assertion passes because both are the same None object -> Option C
  4. Quick Check:

    None is None always True [OK]
Quick Trick: Remember: None is always the same object in Python [OK]
Common Mistakes:
MISTAKES
  • Thinking None creates new objects each time
  • Confusing identity with equality
  • Expecting assertion failure for None identity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes