I have a supposedly simple situation where I am trying to sort a list that contains a mix of class instances, and None. I've implemented the lt method of the class, but I'm still getting an error:
TypeError: '<' not supported between instances of 'NoneType' and 'test_class
Here's how I'm currently doing it:
class test_class:
def __init__(self, num):
self.num = num
def __lt__(self, other):
if other is None:
return False
else:
return self.num < other.num
def __eq__(self, other):
if other is None:
return False
else:
return self.num == other.num
tc1 = test_class(1)
tc2 = test_class(2)
sorted([tc1, tc2, None])
... which produces the above-mentioned error. Could anyone kindly point out what I'm doing wrong? In some sort of idealized reality where programming languages work in a common-sense way, I would have thought that the 'if other is None' bit should handle a comparison with None.
Thanks in advance!