Re: A proposal to modify `None` so that it hashes to a constant

Oscar Benjamin <[email protected]> Mon, 28 Nov 2022 23:13:34 +0000
Newsgroups gmane.comp.python.devel
Message-ID <CAHVvXxTvuyp7J88E3nzmMmKtxg--Y9n8NqwiSEMyswgF5JJbVw@mail.gmail.com>
On Mon, 28 Nov 2022 at 22:56, Brett Cannon <[email protected]> wrote:
>
> On Sun, Nov 27, 2022 at 11:36 AM Yoni Lavi <[email protected]> wrote:
>>
>> All it takes is for your program to compute a set somewhere with affected keys, and iterate on it - and determinism is lost.
>
> That's actually by design. Sets are not meant to be deterministic conceptually as they are essentially a bag of stuff. If you want deterministic ordering you should convert it to a list and sort the list.

What does "sets are not meant to be deterministic" even mean?

Mathematically speaking sets are not meant to be ordered in any
particular way but a computational implementation has to have some
order and there is no reason to prefer non-deterministic order in
general. Actually determinism in a computational context is usually a
very valuable feature. I find it hard to see why non-determinism is
"by design".

Also it isn't usually possible to sort a list containing None:

In [9]: sorted([None, 1, 2])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-344383189210> in <module>
----> 1 sorted([None, 1, 2])

TypeError: '<' not supported between instances of 'int' and 'NoneType'

It would be useful to have a straight-forward way to sort a set into a
deterministic ordering but no such feature exists after the Py3K
changes (sorted used to do this in Python 2.x).

--
Oscar