# Singletons: use 'is'print(TrueisTrue)
print(NoneisNone)
# Small integer caching (-5 to 256)
a = 256
b = 256print(f"256 is 256: {a is b}") # True (cached)
a = 257
b = 257print(f"257 is 257: {a is b}") # May be False!# String interning
s1 = "hello"
s2 = "hello"print(f'"hello" is "hello": {s1 is s2}') # Usually True (interned)# == can be overridden, is cannotclassAlwaysEqual:
def__eq__(self, other):
returnTrue
obj = AlwaysEqual()
print(f"obj == 42: {obj == 42}")
print(f"obj is 42: {obj is 42}")
Output
Click "Run" to execute your code
'is' is a pointer comparison that cannot be overridden. '==' calls __eq__ which can return anything. Use 'is' for singletons, '==' for value comparison.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?