Hello all,

I have a suggestion/idea for the Python standard (and/or a CPython
implementation detail, considering its memory impact): having sets behave
similar to dictionaries in that they preserve first-appearance order (i.e.
dictionaries are ordered by key which was first inserted).

As is said in the Python standard, a 'set' is by definition unordered. Yet,
internally they must be stored in some order, and when iterated upon,
converted to string, etc, some ordering must be used. Right now, it is
unspecified, which can lead to interesting results I noticed:

>>> print ({1, 2, 3}, {3, 2, 1})
{1, 2, 3} {1, 2, 3}
>>> print ({1, 5, 9}, {9, 5, 1})
{1, 5, 9} {9, 5, 1}

While, obviously, things like `A == B` are not affected by this, many
algorithms may affect their output if sets are treated as iterables (again,
the argument could be made, as it was for 'dict' as well, that they should
not treat the order as relevant at all). Further, I believe that having a
specification regarding the order and some guarantees could be useful and
important to cohesiveness of Python as a whole.

Despite the obvious non-ordered nature of sets, I still think it is worth
making them behave like `dict` objects (which are also, in some sense,
'unordered' by nature, and yet Python still specifies the order for 3.7+).

I would like to suggests that `set` objects are ordered by insertion, so
that:
  * Sets have a defined, repeatable order for maximum reproducibility
(assuming the code generates the set in a stable way)
  * Tests which are outside of Python can do string comparisons and expect
the same output each time (as with `dict` objects generated in a
predictable way).
  * On a high-level note, Python specifies more exact behavior. It is my
belief (and I'm sure many others share this as well) that
unspecified/implementation-dependent/otherwise-undependable features should
be eliminated and replaced with exact semantics that do not suprise users.

Thoughts?


Thanks,
~
Cade Brown
Working on MAGMA <https://icl.cs.utk.edu/magma/>
http://cade.site
http://chemicaldevelopment.us
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JEMEOMEPR65PDSSCOULKU2K2S7NUIGRL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to