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

Andy Kish agkish at gmail.com
Thu Jul 23 06:59:12 CEST 2009


Hi all,

I think the addition of a universal set object would be a nice touch
for python sets. 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

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

Instead you have to do something contorted like:

    if len(sets) == 0:
        set_intersection = set()
    else:
        sets_i = iter(sets)
        set_intersection = sets_i.next()
        for s in sets:
            set_intersection &= s

Implementation of a universal set would be pretty trivial. Trails of
EasyExtend [1] has an implementation (albeit used a bit differently)
that's basically the entirety of what's needed.

The above intersection case would end up looking something like:

    set_intersection = set.universal()
    for s in sets:
        set_intersection &= s

Thoughts? I'm happy to write the patch myself if people like this
idea.

Thanks,
Andy Kish.

[1] http://fiber-space.de/wordpress/?p=322



More information about the Python-ideas mailing list