[Python-ideas] Add orderedset as set(iterable, *, ordered=False) and similarly for frozenset.

Stephen J. Turnbull stephen at xemacs.org
Sun Feb 8 17:05:42 CET 2015


Chris Angelico writes:

 > ISTM ordered set addition/union should be like list concatenation with
 > duplicates dropped.

You can't have that, because you lose the order of common elements
that are ordered differently in some of the operands.  I have no idea
why you would want a more or less "symmetric" combination of ordered
sets to ignore the order of elements in any of the operands.  (I write
"symmetric" because I expect the collections in addition or union,
especially union, to be treated "similarly" even if the operations
aren't strictly commutative.)

I could see union defined as "forget the orders and return the union
as sets" but that would surely surprise many users of ordered sets.

Set addition has a mathematical definition, as the "disjoint union"
(coproduct, sort of a multiset that remembers that duplicates came
from different places).  "Practicality before purity" suggests that
most Python programmers won't know what a disjoint union is (and
probably have never heard of "coproduct"), so I'm only -0.5 on using
the "+" operator for this operation, but I still don't see a
"canonical" use case.

I don't have a problem with defining update any way that a reasonably
large plurality of use cases find useful (Andrew's "unsorted but
ordered union" seems reasonable), but it should be called "update" or
"merge" (by analogy with dictionaries or sequences, respectively).

... but don't ask me to come up with the canonical use case, because I
can't think of any at all.


 



More information about the Python-ideas mailing list