[Python-ideas] universal set object for use in set manipulation

Steven D'Aprano steve at pearwood.info
Thu Jul 23 17:26:08 CEST 2009


On Thu, 23 Jul 2009 04:28:33 pm Andy Kish wrote:
> Hi all,
>
> I think the addition of a universal set object would be a nice touch
> for python sets. 

But you can't have a universal set until you know what the problem 
domain is. If the problem domain is citrus fruits, then the universal 
set is: {lemons, limes, grapefruits, oranges, mandarins, tangelos, 
kumquats, ... }

If the problem domain is Presidents of the USA, then the universal set 
is: {Washington, Lincoln, Bush, Clinton, Obama, Wilson, ... }

If the problem domain is integers, then it is {0, 1, -1, 2, -2, 
3, -3, ...}

The first two are finite, the third is infinite.


> Manipulation of collections of sets becomes much 
> easier with it. Right now, certain operations are easy because empty
> sets are easy to create while the dual operation requires special
> cases:
>
>     set_union = set()
>     for s in sets:
>         set_union |= s

Better written as:

>>> reduce(operator.or_, [set([1, 2, 3]), set([2, 4, 5])])
set([1, 2, 3, 4, 5])


>     # this doesn't work
>     set_intersection = set(???)
>     for s in sets:
>         set_intersection &= s

But this does:

>>> reduce(operator.and_, [set([1, 2, 3]), set([2, 4, 5])])
set([2])


> Implementation of a universal set would be pretty trivial.

You think so? I can think of a number of problems.

(1) What elements should go into the universal set? Strings, ints, 
floats, presidents... ?

(2) What is len(set.universal())?

(3) What does set.universal() print?

(4) What does set.universal().clear() do?

(5) For that matter, union(), copy(), difference(), issubset(), etc?

(The universal set for strings of length one is a subset of the 
universal set for all strings.)

I don't think there is a suitable solution for all of these issues. That 
would mean that set.universal() can't be a real set object, it has to 
be some sort of not-quite-a-set object that doesn't provide the entire 
set interface.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list