[Python-ideas] NAN handling in the statistics module

Chris Angelico rosuav at gmail.com
Sun Jan 6 23:29:08 EST 2019


On Mon, Jan 7, 2019 at 3:19 PM David Mertz <mertz at gnosis.cx> wrote:
>
> OK, let me be more precise.  Obviously if the implementation in a class is:
>
> class Foo:
>     def __lt__(self, other):
>         return random.random() < 0.5
>
>
> Then we aren't going to rely on much.
>
> * If comparison of any two items in a list (under __lt__) is deterministic, is the resulting sort order deterministic? (Pretty sure this is a yes)

If you guarantee that exactly one of "x < y" and "y < x" is true for
any given pair of values from the list, and further guarantee that if
x < y and y < z then x < z, you have a total order. Without those two
guarantees, you could have deterministic comparisons (eg "nan < 5" is
always false, but so is "5 < nan"), but there's no way to truly put
the elements "in order". Defining __lt__ as "rock < paper", "paper <
scissors", "scissors < rock" means that you can't guarantee the sort
order, nor determinism.

Are those guarantees safe for your purposes? If so, sort() is, AIUI,
guaranteed to behave sanely.

ChrisA


More information about the Python-ideas mailing list