Q.Example 5.2:
num1 = 20
id(num1)
1433920576 #identity of num1
num2 = 30 - 10
id(num2)
1433920576 #identity of num2 and num1
#are same as both refers to
#object 20
num1 = 20
id(num1)
1433920576 #identity of num1
num2 = 30 - 10
id(num2)
1433920576 #identity of num2 and num1
#are same as both refers to
#object 20
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Concept understanding — Object Identity
Object Identity: The "Who" vs. the "What"
Imagine you have a favourite coffee mug. You use it every morning. One day, you accidentally drop it and the handle breaks. You glue it back together. Is it still the same mug? Most people would say yes — it's the same mug, just damaged. Now imagine you drop it again and it shatters into a hundred pieces. You buy an identical mug from the same store. It looks exactly the same, weighs the same, and holds the same amount of coffee. Is it the same mug? No. It's a different mug that happens to be identical.
That difference — between "being the same object" and "being an identical copy" — is the heart of object identity.
The Intuition: A Continuity of Existence
In everyday life, we treat an object as "the same" if it has a continuous history. Your mug after the handle broke is the same because it was the same physical thing that went through the change. The new mug from the store is different because it has a separate history — it was manufactured at a different time, from different clay, and never sat on your shelf before.
This intuition carries over into computing, but with a crucial twist: in a computer, there is no "physical stuff" that persists. Everything is just bits. So how do we decide whether two pieces of data are the same object or just identical copies?
The Precise Statement
In computer science, object identity is the property that distinguishes an object from all other objects, regardless of its current state. Two objects are identical (the same object) if and only if they occupy the same memory location or have the same unique identifier. Two objects are equal if they have the same content, even if they are different objects.
Identity ≠ Equality
- Identity: Same object (same memory address / same unique ID)
- Equality: Same value (same data, possibly different objects)
Let's make this concrete with a simple example. Suppose you have two variables, a and b, each holding a person's name.
a = "Alice"
b = "Alice"
Are a and b the same object? That depends on the programming language. In Python, small strings are often interned — the interpreter reuses the same object for efficiency. So a and b might actually point to the same memory location. But in most languages, each assignment creates a new object. So a and b would be different objects that happen to have equal values.
Now consider:
a = "Alice"
b = a
Here, b is assigned the same object as a. They are identical — any change to the object through a will be visible through b (if the object is mutable). They are also equal, because they hold the same value.
Why This Matters
Object identity is not an abstract curiosity. It has real consequences: …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.