Python Equality Gothas
2024-03-19
Gabriel MontpetitI was debugging some code and I found a few gotchas with equality in Python. I thought I would add them here for posterity:
when x is 0.0:
not x
isTrue
x is None
isFalse
when x is 0:
not x
isTrue
x is None
isFalse
when x is 1:
not x
isFalse
x is None
isFalse
when x is 1.0:
not x
isFalse
x is None
isFalse
when x is None:
not x
isTrue
x is None
isTrue
when x is ''
:
not x
isTrue
x is None
isFalse
when x is False
:
not x
isTrue
x is None
isFalse
when x is True
:
not x
isFalse
x is None
isFalse
when x is []
:
not x
isTrue
x is None
isFalse
when x is {}
:
not x
isTrue
x is None
isFalse
when x is ()
:
not x
isTrue
x is None
isFalse
when x is set()
:
not x
isTrue
x is None
isFalse