Why Python’s ‘is’ Returns False for Identical Numbers and Other Gotchas
This article explains why Python’s is operator can yield False for seemingly identical integers, how integer caching and code‑block reuse affect object identity, common pitfalls with re.sub’s count parameter, the character‑based behavior of lstrip, and why list multiplication creates shared references.
1. Understanding the is operator
Three code groups are examined. The first and third groups return True while the second returns False. The difference stems from Python’s integer caching (values in the range [-5, 256] are reused) and the fact that statements entered in the same interactive block share the same object.
# First group
a = 256
b = 256
print(a is b) # True
# Second group
a = 257
b = 257
print(a is b) # False
# Third group
a = 257; b = 257
print(a is b) # TrueUsing id() shows identical addresses for the first and third groups but different addresses for the second:
# ids for first group
print(id(a)) # e.g., 1426657040
print(id(b)) # same as a
# ids for second group
print(id(a)) # e.g., 363389616
print(id(b)) # e.g., 363392912
# ids for third group
print(id(a)) # e.g., 5722000
print(id(b)) # same as aWhen the same value appears in a function’s local scope, a new object is created, so the identity differs:
a = 257
b = 257
def func():
c = 257
print(a is c) # False
print(a is b) # True
func()2. Using re.sub() correctly
The re.sub() function replaces patterns in a string. A common mistake is passing the flag re.S as the count argument, which is interpreted as the integer 16. This limits the substitution to the first 16 matches, leaving later tags untouched.
import re
def remove_tag(html):
text = re.sub(r'<.*?>', '', html, re.S)
return textExample with a short HTML snippet:
html = """
<!DOCTYPE html><html lang='en'>
<head><meta charset='UTF-8'>
<title>Document</title>
</head><body></body></html>
"""
print(remove_tag(html)) # DocumentWith a longer, malformed HTML string, the last two tags remain because only the first 16 matches are replaced:
html = """
<!Dtp-equiv='X-UA-Compatible' content='ie=edge'><title>Document</title>
</head><bodOCTYPE html><html lang='en'><head><meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta hty><h1>h1标题</h1><h2>h2标题</h2><h3>h3标题</h3></body></html>
"""
print(remove_tag(html))
# Output shows the trailing </body></html> tags were not removed3. How lstrip() works
The lstrip() method treats its argument as a set of characters, not as a substring. It removes any leading characters that appear in that set until a character not in the set is encountered.
print("aabbcc".lstrip('aa')) # bbcc
print("ababacac".lstrip('ab')) # cacTo remove a specific prefix, use replace() or slice the string instead.
4. Pitfalls of nested list creation
Creating a list of empty lists with multiplication creates multiple references to the same inner list. Modifying one reference affects all.
# Option 1 (list comprehension) – creates independent lists
li = [[] for i in range(3)]
# Option 3 (multiplication) – creates three references to the same list
li = [[]] * 3
li[0].append(1)
print(li) # [[1], [1], [1]]The same issue appears when using the multiplication syntax directly:
li = [[]] * 3
li[0].append(1)
print(li) # [[1], [1], [1]]Use a list comprehension or list() with a generator to obtain distinct inner lists.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
