I stumbled across this while working on a heap implementation myself. :)
As of Python 3.5, you can use the inf constant from the math module
from math import inf
inf + inf # inf
inf - inf # nan
inf / inf # nan
inf * inf # inf
max(list(range(1_000_000)) + [inf]) # inf
min(list(range(-1_000_000, 1)) + [-inf]) # -inf
I did not know this and used a custom class to achieve the same ordering property
class MinusInf:
def __gt__(self, other):
return False
def __ge__(self):
return False
def __lt__(self, other):
return True
def __le__(self, other):
return True
def __eq__(self, other):
return False
minus_inf = MinusInf()
minus_inf >= -1_000_000 # False
This will work for the purpose of a heap, but the recommended way is to just use math.inf (or numpy.inf which is the inf constant from numpy).