The Secret Life of Python: The Identity Crisis ('==' and 'is')
Why == and is are not the same thing in Python.
Timothy was staring at his screen, looking like he had seen a ghost. "Margaret," he whispered. "I think I found a glitch in the matrix."
Margaret looked up from her book. "The matrix is usually quite stable, Timothy. What did you find?"
"I have two lists," Timothy said. "They are exactly the same. I checked them three times. But Python says they are different."
He pointed to his code:
# Timothy's Configuration Check
# The default configuration for the app
default_config = ["dark_mode", "notifications_on"]
# The user's current configuration (which happens to be identical)
user_config = ["dark_mode", "notifications_on"]
# Timothy checks if they are the same
if user_config == default_config:
print("They look the same!")
if user_config is default_config:
print("They are the same object!")
else:
print("They are strangers!")
Output:
They look the same!
They are strangers!
"See?" Timothy argued. "It admits they look the same. But then it says user_config is default_config is False. How can they be the same and not the same at once?"
The Twins Paradox
Margaret smiled. "This isn't a glitch. It is a question of Identity versus Equality."
She walked to the whiteboard and sketched a parking lot.
"Imagine you see two red sports cars in a parking lot," she began. "They are the same make, same model, same year, same bright red paint. If you ask, 'Are these cars equal?' the answer is Yes. They have the same attributes."
"But," she continued, "if you ask, 'Is the car on the left the same object as the car on the right?' the answer is No."
"Because they are two different physical cars," Timothy realized.
"Exactly," Margaret said. "When you typed [], you told Python to build a new list. You gave that command twice, so Python built two separate cars."
"In Python," she summarized, "the == operator asks: 'Do these things look the same?' (Equality). But the is operator asks: 'Are these the exact same object in memory?' (Identity)."
The Fingerprint Scanner
"But how can I prove it?" Timothy asked. "In code, they look identical."
"Every object in Python has a secret ID number," Margaret explained. "Think of it like a VIN number on a car, or a fingerprint. We can see it using the id() function."
She modified his code to reveal the truth:
print(f"ID of default_config: {id(default_config)}")
print(f"ID of user_config: {id(user_config)}")
Output:
ID of default_config: 140234567890128
ID of user_config: 140234567890896
Timothy’s eyes went wide. "The numbers are different! The last three digits..."
"Exactly," Margaret pointed to the screen. "Those numbers are their addresses in memory. Even though they contain the exact same text, they live in two different houses. They are twins, not the same person."
When is becomes True
"So when would is ever be True?" Timothy asked.
"Only when you are pointing to the exact same object," Margaret said. She added a few lines to the script.
# Create an alias (Reference)
my_settings = user_config
if my_settings is user_config:
print("These are the SAME object!")
Output:
These are the SAME object!
"Ah!" Timothy said. "Because I didn't use [] to make a new list. I just gave user_config a second nametag."
Margaret’s Cheat Sheet
Margaret opened her notebook to the "Comparisons" section.
- The Question: "Are these the same?"
- The Operator
==(Equality): Checks if the values are the same. Example:
[1, 2] == [1, 2]isTrue. (Same data).The Operator
is(Identity): Checks if they are the same object in memory.Example:
[1, 2] is [1, 2]isFalse. (Two different lists).The Proof: Use
id(x)to see the unique memory address.The Safety Warning: Python sometimes reuses objects for small numbers (like
5or100) to save memory. Because of this,ismight act weirdly with numbers.Rule of Thumb: Always use
==for numbers and strings. Only useisforTrue,False, orNone.
Timothy looked at his two lists again. "Twins," he muttered. "Identical twins, but strangers."
"Precisely," Margaret smiled. "And now you know how to tell them apart."
In the next episode, Margaret and Timothy will face "The Skipping Stone"—where Timothy tries to remove items from a list while looping through it, and watches as Python skips over items in a way that defies logic.
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.
.jpeg)

Comments
Post a Comment