Comparison to singleton
False
should be expr is False
43 MonitoringStation(
44 "111", "111", "station1", (0.0, 1.0), (1, -4), "River 1", "Town 1"
45 ).typical_range_consistent()
46 == False 47 )
48 assert (
49 MonitoringStation(
Comparison to singleton
False
should be expr is False
49 MonitoringStation(
50 "111", "111", "station1", (0.0, 1.0), (0.5, 0.2), "River 1", "Town 1"
51 ).typical_range_consistent()
52 == False 53 )
54
55 def test_inconsistent_typical_range_stations(self):
Comparison to singleton
True
should be expr is True
31 MonitoringStation(
32 "111", "111", "station1", (0.0, 1.0), (1.0, 4.0), "River 1", "Town 1"
33 ).typical_range_consistent()
34 == True 35 )
36 assert (
37 MonitoringStation(
Comparison to singleton
False
should be expr is False
37 MonitoringStation(
38 "111", "111", "station1", (0.0, 1.0), None, "River 1", "Town 1"
39 ).typical_range_consistent()
40 == False 41 )
42 assert (
43 MonitoringStation(
Description
Comparisons to the singleton objects, like True
, False
, and None
, should be done with identity, not equality. Use is
or is not
.
Identity checks are faster than equality checks.
Also, the equality checks can result in unintended behaviour in some cases:
>>> 1 is True
False
>>> 1 == True
True
>>> 1.0 is True
False
>>> 1.0 == True
True
Bad practice
value = fx()
if value == True: # `==` can cause unintended behaviour
print(value)
if value != None:
print(value)
Recommended
value = fx()
if value is True: # `is` for Identity comparison
print(value)
if value is not None:
print(value)
Exceptions:
Equality comparisons might be required in some cases, especially in SQLAlchemy, SQLObject, ORMs, etc. In these cases, it is safe to ignore this issue.