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

Neil Girdhar mistersheik at gmail.com
Sun Feb 8 00:10:52 CET 2015


On Sat, Feb 7, 2015 at 6:02 PM, Ed Kellett <edk141 at gmail.com> wrote:

> On Sat Feb 07 2015 at 10:37:39 PM Chris Angelico <rosuav at gmail.com> wrote:
>
>> Set union is, but sets lack order. Once you add order to a set, it
>> becomes more like a list
>
>
> Then wouldn't adding .extend() and + make more sense anyway? List-like
> operations should look like the list-like operations we already have.
>
>
>> so the union of x and y might not be identical to the union of y and x
>
>
> Well, not if you define union to be non-commutative, no—but that's what
> we're discussing.
>
>
>> They'd be the same modulo order, so it's not breaking the concept of set
>> union.
>
>
> Sure it is: operations are supposed to yield things that make sense. Set
> union yields the set of elements contained in any of its operands; even if
> its operands have orderings that make sense, the ordering between sets
> might be partial or inconsistent, so the result cannot. So the existing
> definition of set union can never produce a correct ordering.
>
> I can't see why it would be preferable to redefine union as concatenation
> rather than just supporting explicit concatenation. Apart from feeling more
> mathematically sound to me, it's better at documenting intention: which
> method (or operator) you choose reflects whether you're interested in the
> order of the result or just its contents.
>

The reason is that that's already what update does, which is the most
common way to use OrderedSet.  Typically, you have a bunch of things and
you want a set of unique elements in the order they were added.  So you
create your accumulator and then update it with iterables of elements,
after which it contains your desired ordered set of unique elements.  Maybe
your code looks like this:

a = OrderedSet()

a.update(b)

a.update(c)

a.update(d)

Why shouldn't that be the same as

a | b | c | d

?

I think it should and that in general union should be equivalent in effect
to copy and extend.

Best,

Neil

>
> Ed Kellett
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/4WZbMC2pNe0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/4WZbMC2pNe0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150207/7b77aad5/attachment.html>


More information about the Python-ideas mailing list