
On Mon, Nov 28, 2022 at 5:38 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Nov 28, 2022 at 11:13:34PM +0000, Oscar Benjamin wrote:
On Mon, 28 Nov 2022 at 22:56, Brett Cannon <brett@python.org> wrote:
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?
I'm not Brett, so I'm not answering for him, but many people (sometimes including me) misuse "not deterministic" to refer to set order and the old dict order.
Yep, that's what I mean. And my comment about dicts earlier is old knowledge from before we made dict iteration deterministic based on insertion order since I'm been doing this for too long. 😉 -Brett
Of course set order is deterministic, it is determined by the combination of the set implementation, the hashing algorithm, and the history of the set -- all the items that have ever appeared in the set (both those removed and those that remain).
Set order is deterministic in the same way that roulette wheels are deterministic.
We don't have a good term for this: the items don't appear in random order. But it is not predictable either, except in very special cases. "Arbitrary" is not right either, since that implies we can easily choose whatever set order we want.
I think that physicists call this "deterministic chaos"
https://www.quora.com/Theoretical-Physics-What-is-deterministic-but-unpredic...
(sorry for the quora link) so I guess we might say that set iteration order is "deterministically chaotic" if you want to be precise, but life is too short for that level of pedantry (and that's coming from me, a Grade A Pedant *wink*) so people often describe it as random, arbitrary, or non-deterministic when it's none of those things :-).
Maybe we could call set order "pseudo-random".
Getting back to the design part, I think that what Brett is trying to get across is not that the chaotic set order is in and of itself a requirement, but that given the other requirements, chaotic set order is currently considered a necessary condition.
As I understand it, we could make sets ordered, but only at the cost of space (much more memory) or time (slower) or both.
I am sure that Guido is correct that **if** somebody comes up with a fast, efficient ordered set implementation that doesn't perform worse than the current implementation, we will happily swap to giving sets a predictable order, as we did with dicts. (Practicality beats purity -- even if sets are *philosophically* unordered, preserving input order is too useful to give up unless we gain something in return.)
But I don't think it is fair or kind to call Brett's argument FUD. At the very least it is uncharitable interpretation of Brett's position.
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).
`sorted()` works fine on homogeneous sets. It is only heterogeneous sets that are a problem, and in practice, that usually means None mixed in with some other type.
-- Steve _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/UAVNSMS3... Code of Conduct: http://python.org/psf/codeofconduct/