[Python-Dev] syntactic support for sets

Josiah Carlson jcarlson at uci.edu
Mon Feb 6 19:39:38 CET 2006

Donovan Baarda <abo at minkirri.apana.org.au> wrote:
> On Fri, 2006-02-03 at 11:56 -0800, Josiah Carlson wrote:
> > Along the lines of "not every x line function should be a builtin", "not
> > every builtin should have syntax".  I think that sets have particular
> > uses, but I don't believe those uses are sufficiently varied enough to
> > warrant the creation of a syntax.  I suggest that people take a walk
> > through their code. How often do you use other sequence and/or mapping
> > types? How many lists, tuples and dicts are there?  How many sets? Ok,
> > now how many set literals?
> The absence of sets in early Python, the requirement to "import sets"
> when they first appeared, and the lack of a set syntax now all mean that
> people tend to avoid using sets and resort to lists, tuples, and "dicts
> of None" instead, even though they really want a set. Anywhere you see
> "if value in sequence:", they probably mean sequence is a set, and this
> code would run much faster if it really was, and might even avoid
> potential bugs because it would prevent duplicates...

Maybe they mean set, maybe they don't.  'if obj in seq' is used for
various reasons.

A quick check of the Python standard library shows that some of the uses
of 'if obj in tuple_literal' could certainly be converted into sets, but
that ignores the performance impact of using sets instead of short
tuples (where short, if I remember correctly, is a length of 3, check
the python-dev archives), as well as the module-level contant creation
that occurs with tuples. There was probably a good reason why such a
thing hasn't happened with lists and dicts (according to my Python 2.4
installation), and why it may not happen with sets.

A nontrivial number of other 'if obj in seq' instances actually need
dictionaries, the test is for some sort of data handler or headers with
a particular name.

 - Josiah

More information about the Python-Dev mailing list