Great examples! These are all Python "quirks" that often trip people up but are actually rooted in sensible (if sometimes surprising) design decisions. Here's a breakdown of each one, along with whether I think it's sensible and why it exists.
1.a = 10; b = 10; a is bvs.a = 500; b = 500; a is b
Explanation:
a is bchecksobject identity, not equality.
For small integers (typically between-5 and 256), Python caches the objects (calledinteger interning) to save memory and speed up access.
10is interned, soaandbpoint to the same object.
500isnotinterned by default, soaandbrefer to different objects.
a is bchecksobject identity, not equality.
For small integers (typically between-5 and 256), Python caches the objects (calledinteger interning) to save memory and speed up access.
10is interned, soaandbpoint to the same object.
500isnotinterned by default, soaandbrefer to different objects.
Sensible?
Yes, mostly.
It’s an optimization to avoid repeatedly allocating small integer objects, which are used a lot.
It avoids memory overhead and speeds things up for typical use cases.
It’s an optimization to avoid repeatedly allocating small integer objects, which are used a lot.
It avoids memory overhead and speeds things up for typical use cases.
Why not fixed?
Becauseisisintentionallyabout identity. This is working as designed. If you want value comparison, use==.
2.all([]),all([[]]),all([[[]]])
Explanation:
all([])→TruebecauseallreturnsTrueonempty iterables(vacuous truth: "no element is False").
all([[]])→Falsebecause the inner list[]isfalsy, soall()returnsFalse.
list with one empty list inside) , even though it contains an empty list.
all([])→TruebecauseallreturnsTrueonempty iterables(vacuous truth: "no element is False").
all([[]])→Falsebecause the inner list[]isfalsy, soall()returnsFalse.
list with one empty list inside) , even though it contains an empty list.
Sensible?
Yes, but surprising.
It’s consistent with Python’s general truthiness rules.
An empty list isFalse, but a list withsomething in it(even another empty list) isTrue.
It’s consistent with Python’s general truthiness rules.
An empty list isFalse, but a list withsomething in it(even another empty list) isTrue.
Why not fixed?
It’s actually mathematically consistent. Changing this would break theallandanybehavior for other iterables, which are widely relied on.
- Dictionary with{1: 'a', True: 'b', 1.0: 'c'}
Explanation:
Python treats1,True, and1.0asequalwhen used as dictionary keys because:True == 1isTrue1 == 1.0isTrue
True == 1isTrue
1 == 1.0isTrue
Python dicts usehash values + equalityto determine key uniqueness.All three have the samehash(), so they overwrite each other.The last value wins, so you're left with{1: 'c'}.
All three have the samehash(), so they overwrite each other.
The last value wins, so you're left with{1: 'c'}.
Python treats1,True, and1.0asequalwhen used as dictionary keys because:
True == 1isTrue
1 == 1.0isTrue
True == 1isTrue
1 == 1.0isTrue
Python dicts usehash values + equalityto determine key uniqueness.
All three have the samehash(), so they overwrite each other.
The last value wins, so you're left with{1: 'c'}.
All three have the samehash(), so they overwrite each other.
The last value wins, so you're left with{1: 'c'}.
Sensible?
Debatable.
Sensibleif you consider Python’s design thatTrue == 1.
Confusingfor people who expect type distinctions in dict keys.
But changing this would require breaking backwards compatibility in core type behavior (==).
Sensibleif you consider Python’s design thatTrue == 1.
Confusingfor people who expect type distinctions in dict keys.
But changing this would require breaking backwards compatibility in core type behavior (==).
Why not fixed?
It’s deeply tied to Python’s type system and backwards compatibility. Fixing this would cause too many breakages in existing code.
Bottom Line:
These all seemweirdat first, but they follow consistent internal rules:
Object identity vs value comparison
Truthiness of containers
Equality and hashing behavior of built-in types
Object identity vs value comparison
Truthiness of containers
Equality and hashing behavior of built-in types
Python prioritizes consistency and performance over surprise, even if that means being a little unintuitive sometimes. Once you learn the rules, it becomes easier—and predictable.
Want me to throw together a visual or analogy for any of these?