On Sat, Jun 2, 2018 at 8:40 PM, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > Python's sort algorithm assumes that objects have consistent, sensible > comparisons. If you write an object like this: > > > class Rubbish: > def __eq__(self, other): > return random.random() > 0.5 > def __lt__(self, other): > return random.random() > 0.5 > def __gt__(self, other): > return random.random() > 0.5 > > it too will mess up sorting in unpredictable ways. So don't do that. >
Point of curiosity: Why "> 0.5"? Normally when I want a fractional chance, I write the comparison the other way: "random.random() < 0.5" has exactly a 50% chance of occurring (presuming that random.random() follows its correct documented distribution). I've no idea what the probability of random.random() returning exactly 0.5 is, but since it can return 0.0 and cannot return 1.0, I've just always used less-than. (Also because it works nicely with other values - there's a 30% chance that random.random() is less than 0.3, etc.) Is there a reason for going greater-than, or is it simply that it doesn't matter? ChrisA -- https://mail.python.org/mailman/listinfo/python-list