Interface of the set classes

Alex Martelli aleaxit at yahoo.com
Fri Oct 29 09:45:52 EDT 2004


Pierre Barbier de Reuille <pierre.barbier at cirad.fr> wrote:

> Ok, I first want to stress that I looked through the newsgroups archives
> (on Google) for an answer to my question, but I didn't find it.
> 
> It's about the interface of the set classes as defined in the PEP 218.

You should check out the built-in set type in 2.4 (the beta is quite
usable, go ahead and get it!) -- dunno if PEP 218 has been updated to
reflect exactly what's ended up in the language.

> I don't understand why the sets has an interface of mapping object 
> without associated value and not an interface of sequence ?

Sequences have order, sets don't.

> Then, why can't you at all access sets element by position ? As Python
> iterator are so poor (mainly, you cannot copy them ...) you have things

That's what itertools.tee is for (in Python 2.4).

> you cannot do without positionnal iteration (typically when you need to
> store the current position and go on the iteration to restart from the
> saved position later on).

Yep, the quintessential usecase of itertools.tee.

> Please, if this discussion already exists give me the pointer ... and
> don't tell me "because of implementation if over dictionnary" ... I 
> don't think that can be a good answer :o)

It would be false (in 2.4) -- set is a builtin type, side by side with
list and dict, not relying on either of them.


It may well be that, for whatever needs, you need a different container
type.  That's what the collections module in the standard library is for
(again, in 2.4) -- in 2.4 there's only deque in it, but that's the place
where, in 2.5, other collection types may go.  In my case, the main
reason I find myself using something other than set, for example, is
when I have non-hashable items.

The best way to get new collection types into Python 2.5 is to write
them now (pure Python, pyrex, C, whatever), publish them (Activestate
cookbook, sourceforge, whatever), get a user-base so that experience can
enhance them, _and_ also get involved in python-dev to make a PEP and
champion it.  No hurry, it's probably 18+ months to go to 2.5, but the
herein-suggested, soundest process to make it likely that one's changes,
in the best possible form, will get into 2.5's standard library, does
take its time, too;-)


Alex



More information about the Python-list mailing list