[Python-3000] sets in P3K?

Gareth McCaughan gmccaughan at synaptics-uk.com
Thu Apr 27 13:17:58 CEST 2006


On Thursday 2006-04-27 11:42, Georg Brandl wrote:
> Kay Schluehr wrote:
...
> > Furthermore: what about notions of infinite sets?
> 
> Can you elaborate on that?

Seems to me that if you want infinite sets, you want some type
other than "set". Ah, the wonders of duck typing. (Just as if
you want mappings with infinite domain, you want a type other
than "dict".)

Still, there's one generalization that's not 100% crazy (though
quite possibly 90%): allow *cofinite* sets; that is, ones whose
complement is finite. Then you can express "everything except 1"
as, say,

    ~{1}.

The nice thing is that { finite and cofinite sets } is, just like
{ finite sets }, closed under the usual operations, so you can
still do things like unions and intersections, and you also get
the complement operator (which I was proposing should be ~ rather
than "not", by analogy with bitwise ops).

Two objections, which between them I think are probably sufficient
to make this a Bad Idea.

Firstly, you can't iterate over a cofinite set. Secondly and, I think,
more importantly, in practical use any given set is going to be known
in advance to be finite, or known in advance to be cofinite, and
your algorithm is unlikely to be made less clear by working with
the complement of that set when it's cofinite. So the benefit isn't
altogether clear.

-- 
g



More information about the Python-3000 mailing list